OSC: Add /strip/fader/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->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
2346                 }
2347         }
2348
2349         return ret;
2350 }
2351
2352 int
2353 OSC::route_mute (int ssid, int yn, lo_message msg)
2354 {
2355         if (!session) return -1;
2356         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2357
2358         if (s) {
2359                 if (s->mute_control()) {
2360                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2361                         return 0;
2362                 }
2363         }
2364
2365         return route_send_fail ("mute", ssid, 0, get_address (msg));
2366 }
2367
2368 int
2369 OSC::sel_mute (uint32_t yn, lo_message msg)
2370 {
2371         OSCSurface *sur = get_surface(get_address (msg));
2372         boost::shared_ptr<Stripable> s;
2373         if (sur->expand_enable) {
2374                 s = get_strip (sur->expand, get_address (msg));
2375         } else {
2376                 s = _select;
2377         }
2378         if (s) {
2379                 if (s->mute_control()) {
2380                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2381                         return 0;
2382                 }
2383         }
2384         return sel_fail ("mute", 0, get_address (msg));
2385 }
2386
2387 int
2388 OSC::route_solo (int ssid, int yn, lo_message msg)
2389 {
2390         if (!session) return -1;
2391         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2392
2393         if (s) {
2394                 if (s->solo_control()) {
2395                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2396                 }
2397         }
2398
2399         return route_send_fail ("solo", ssid, 0, get_address (msg));
2400 }
2401
2402 int
2403 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2404 {
2405         if (!session) return -1;
2406         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2407
2408         if (s) {
2409                 if (s->solo_isolate_control()) {
2410                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2411                         return 0;
2412                 }
2413         }
2414
2415         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2416 }
2417
2418 int
2419 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2420 {
2421         if (!session) return -1;
2422         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2423
2424         if (s) {
2425                 if (s->solo_safe_control()) {
2426                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2427                         return 0;
2428                 }
2429         }
2430
2431         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2432 }
2433
2434 int
2435 OSC::sel_solo (uint32_t yn, lo_message msg)
2436 {
2437         OSCSurface *sur = get_surface(get_address (msg));
2438         boost::shared_ptr<Stripable> s;
2439         if (sur->expand_enable) {
2440                 s = get_strip (sur->expand, get_address (msg));
2441         } else {
2442                 s = _select;
2443         }
2444         if (s) {
2445                 if (s->solo_control()) {
2446                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2447                 }
2448         }
2449         return sel_fail ("solo", 0, get_address (msg));
2450 }
2451
2452 int
2453 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2454 {
2455         OSCSurface *sur = get_surface(get_address (msg));
2456         boost::shared_ptr<Stripable> s;
2457         if (sur->expand_enable) {
2458                 s = get_strip (sur->expand, get_address (msg));
2459         } else {
2460                 s = _select;
2461         }
2462         if (s) {
2463                 if (s->solo_isolate_control()) {
2464                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2465                         return 0;
2466                 }
2467         }
2468         return sel_fail ("solo_iso", 0, get_address (msg));
2469 }
2470
2471 int
2472 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2473 {
2474         OSCSurface *sur = get_surface(get_address (msg));
2475         boost::shared_ptr<Stripable> s;
2476         if (sur->expand_enable) {
2477                 s = get_strip (sur->expand, get_address (msg));
2478         } else {
2479                 s = _select;
2480         }
2481         if (s) {
2482                 if (s->solo_safe_control()) {
2483                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2484                         return 0;
2485                 }
2486         }
2487         return sel_fail ("solo_safe", 0, get_address (msg));
2488 }
2489
2490 int
2491 OSC::sel_recenable (uint32_t yn, lo_message msg)
2492 {
2493         OSCSurface *sur = get_surface(get_address (msg));
2494         boost::shared_ptr<Stripable> s;
2495         if (sur->expand_enable) {
2496                 s = get_strip (sur->expand, get_address (msg));
2497         } else {
2498                 s = _select;
2499         }
2500         if (s) {
2501                 if (s->rec_enable_control()) {
2502                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2503                         if (s->rec_enable_control()->get_value()) {
2504                                 return 0;
2505                         }
2506                 }
2507         }
2508         return sel_fail ("recenable", 0, get_address (msg));
2509 }
2510
2511 int
2512 OSC::route_recenable (int ssid, int yn, lo_message msg)
2513 {
2514         if (!session) return -1;
2515         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2516
2517         if (s) {
2518                 if (s->rec_enable_control()) {
2519                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2520                         if (s->rec_enable_control()->get_value()) {
2521                                 return 0;
2522                         }
2523                 }
2524         }
2525         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2526 }
2527
2528 int
2529 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2530     if (!session) {
2531         return -1;
2532     }
2533
2534     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2535
2536     if (s) {
2537         s->set_name(std::string(newname));
2538     }
2539
2540     return 0;
2541 }
2542
2543 int
2544 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2545 {
2546         OSCSurface *sur = get_surface(get_address (msg));
2547         boost::shared_ptr<Stripable> s;
2548         if (sur->expand_enable) {
2549                 s = get_strip (sur->expand, get_address (msg));
2550         } else {
2551                 s = _select;
2552         }
2553         if (s) {
2554                 if (s->rec_safe_control()) {
2555                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2556                         if (s->rec_safe_control()->get_value()) {
2557                                 return 0;
2558                         }
2559                 }
2560         }
2561         return sel_fail ("record_safe", 0, get_address (msg));
2562 }
2563
2564 int
2565 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2566 {
2567         if (!session) return -1;
2568         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2569         if (s) {
2570                 if (s->rec_safe_control()) {
2571                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2572                         if (s->rec_safe_control()->get_value()) {
2573                                 return 0;
2574                         }
2575                 }
2576         }
2577         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2578 }
2579
2580 int
2581 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2582 {
2583         if (!session) return -1;
2584         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2585
2586         if (s) {
2587                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2588                 if (track) {
2589                         if (track->monitoring_control()) {
2590                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2591                                 return 0;
2592                         }
2593                 }
2594         }
2595
2596         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2597 }
2598
2599 int
2600 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2601 {
2602         OSCSurface *sur = get_surface(get_address (msg));
2603         boost::shared_ptr<Stripable> s;
2604         if (sur->expand_enable) {
2605                 s = get_strip (sur->expand, get_address (msg));
2606         } else {
2607                 s = _select;
2608         }
2609         if (s) {
2610                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2611                 if (track) {
2612                         if (track->monitoring_control()) {
2613                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2614                                 return 0;
2615                         }
2616                 }
2617         }
2618         return sel_fail ("monitor_input", 0, get_address (msg));
2619 }
2620
2621 int
2622 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2623 {
2624         if (!session) return -1;
2625         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2626
2627         if (s) {
2628                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2629                 if (track) {
2630                         if (track->monitoring_control()) {
2631                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2632                                 return 0;
2633                         }
2634                 }
2635         }
2636
2637         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2638 }
2639
2640 int
2641 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2642 {
2643         OSCSurface *sur = get_surface(get_address (msg));
2644         boost::shared_ptr<Stripable> s;
2645         if (sur->expand_enable) {
2646                 s = get_strip (sur->expand, get_address (msg));
2647         } else {
2648                 s = _select;
2649         }
2650         if (s) {
2651                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2652                 if (track) {
2653                         if (track->monitoring_control()) {
2654                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2655                                 return 0;
2656                         }
2657                 }
2658         }
2659         return sel_fail ("monitor_disk", 0, get_address (msg));
2660 }
2661
2662
2663 int
2664 OSC::strip_phase (int ssid, int yn, lo_message msg)
2665 {
2666         if (!session) return -1;
2667         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2668
2669         if (s) {
2670                 if (s->phase_control()) {
2671                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2672                         return 0;
2673                 }
2674         }
2675
2676         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2677 }
2678
2679 int
2680 OSC::sel_phase (uint32_t yn, lo_message msg)
2681 {
2682         OSCSurface *sur = get_surface(get_address (msg));
2683         boost::shared_ptr<Stripable> s;
2684         if (sur->expand_enable) {
2685                 s = get_strip (sur->expand, get_address (msg));
2686         } else {
2687                 s = _select;
2688         }
2689         if (s) {
2690                 if (s->phase_control()) {
2691                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2692                         return 0;
2693                 }
2694         }
2695         return sel_fail ("polarity", 0, get_address (msg));
2696 }
2697
2698 int
2699 OSC::strip_expand (int ssid, int yn, lo_message msg)
2700 {
2701         OSCSurface *sur = get_surface(get_address (msg));
2702         sur->expand_enable = (bool) yn;
2703         sur->expand = ssid;
2704         boost::shared_ptr<Stripable> s;
2705         if (yn) {
2706                 s = get_strip (ssid, get_address (msg));
2707         } else {
2708                 s = ControlProtocol::first_selected_stripable();
2709         }
2710
2711         return _strip_select (s, get_address (msg));
2712 }
2713
2714 int
2715 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2716 {
2717         if (!session) {
2718                 return -1;
2719         }
2720         OSCSurface *sur = get_surface(addr);
2721         if (sur->sel_obs) {
2722                 delete sur->sel_obs;
2723                 sur->sel_obs = 0;
2724         }
2725         bool feedback_on = sur->feedback.to_ulong();
2726         if (s && feedback_on) {
2727                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2728                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2729                 sur->sel_obs = sel_fb;
2730         } else if (sur->expand_enable) {
2731                 sur->expand = 0;
2732                 sur->expand_enable = false;
2733                 if (_select && feedback_on) {
2734                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2735                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2736                         sur->sel_obs = sel_fb;
2737                 }
2738         } else if (feedback_on) {
2739                 route_send_fail ("select", sur->expand, 0 , addr);
2740         }
2741         if (!feedback_on) {
2742                 return 0;
2743         }
2744         //update buttons on surface
2745         int b_s = sur->bank_size;
2746         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2747                 b_s = sur->nstrips;
2748         }
2749         for (int i = 1;  i <= b_s; i++) {
2750                 string path = "expand";
2751
2752                 if ((i == (int) sur->expand) && sur->expand_enable) {
2753                         lo_message reply = lo_message_new ();
2754                         if (sur->feedback[2]) {
2755                                 ostringstream os;
2756                                 os << "/strip/" << path << "/" << i;
2757                                 path = os.str();
2758                         } else {
2759                                 ostringstream os;
2760                                 os << "/strip/" << path;
2761                                 path = os.str();
2762                                 lo_message_add_int32 (reply, i);
2763                         }
2764                         lo_message_add_float (reply, (float) 1);
2765
2766                         lo_send_message (addr, path.c_str(), reply);
2767                         lo_message_free (reply);
2768                         reply = lo_message_new ();
2769                         lo_message_add_float (reply, 1.0);
2770                         lo_send_message (addr, "/select/expand", reply);
2771                         lo_message_free (reply);
2772
2773                 } else {
2774                         lo_message reply = lo_message_new ();
2775                         lo_message_add_int32 (reply, i);
2776                         lo_message_add_float (reply, 0.0);
2777                         lo_send_message (addr, "/strip/expand", reply);
2778                         lo_message_free (reply);
2779                 }
2780         }
2781         if (!sur->expand_enable) {
2782                 lo_message reply = lo_message_new ();
2783                 lo_message_add_float (reply, 0.0);
2784                 lo_send_message (addr, "/select/expand", reply);
2785                 lo_message_free (reply);
2786         }
2787
2788         return 0;
2789 }
2790
2791 int
2792 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2793 {
2794         //ignore button release
2795         if (!yn) return 0;
2796
2797         if (!session) {
2798                 return -1;
2799         }
2800         OSCSurface *sur = get_surface(get_address (msg));
2801         sur->expand_enable = false;
2802         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2803         if (s) {
2804                 SetStripableSelection (s);
2805         } else {
2806                 if ((int) (sur->feedback.to_ulong())) {
2807                         route_send_fail ("select", ssid, 0, get_address (msg));
2808                 }
2809         }
2810
2811         return 0;
2812 }
2813
2814 int
2815 OSC::sel_expand (uint32_t state, lo_message msg)
2816 {
2817         OSCSurface *sur = get_surface(get_address (msg));
2818         boost::shared_ptr<Stripable> s;
2819         sur->expand_enable = (bool) state;
2820         if (state && sur->expand) {
2821                 s = get_strip (sur->expand, get_address (msg));
2822         } else {
2823                 s = ControlProtocol::first_selected_stripable();
2824         }
2825
2826         return _strip_select (s, get_address (msg));
2827 }
2828
2829 int
2830 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2831 {
2832         if (!session) return -1;
2833         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2834
2835         if (s) {
2836                 if (s->gain_control()) {
2837                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2838                 } else {
2839                         return 1;
2840                 }
2841         } else {
2842                 return 1;
2843         }
2844
2845         return 0;
2846 }
2847
2848 int
2849 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2850 {
2851         if (!session) {
2852                 route_send_fail ("gain", ssid, -193, get_address (msg));
2853                 return -1;
2854         }
2855         int ret;
2856         if (dB < -192) {
2857                 ret = route_set_gain_abs (ssid, 0.0, msg);
2858         } else {
2859                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2860         }
2861         if (ret != 0) {
2862                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2863         }
2864         return 0;
2865 }
2866
2867 int
2868 OSC::sel_gain (float val, lo_message msg)
2869 {
2870         OSCSurface *sur = get_surface(get_address (msg));
2871         boost::shared_ptr<Stripable> s;
2872         if (sur->expand_enable) {
2873                 s = get_strip (sur->expand, get_address (msg));
2874         } else {
2875                 s = _select;
2876         }
2877         if (s) {
2878                 float abs;
2879                 if (val < -192) {
2880                         abs = 0;
2881                 } else {
2882                         abs = dB_to_coefficient (val);
2883                 }
2884                 if (s->gain_control()) {
2885                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2886                         return 0;
2887                 }
2888         }
2889         return sel_fail ("gain", -193, get_address (msg));
2890 }
2891
2892 int
2893 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2894 {
2895         if (!session) {
2896                 route_send_fail ("fader", ssid, 0, get_address (msg));
2897                 return -1;
2898         }
2899         int ret;
2900         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2901         if (ret != 0) {
2902                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2903         }
2904         return 0;
2905 }
2906
2907 int
2908 OSC::sel_fader (float val, lo_message msg)
2909 {
2910         OSCSurface *sur = get_surface(get_address (msg));
2911         boost::shared_ptr<Stripable> s;
2912         if (sur->expand_enable) {
2913                 s = get_strip (sur->expand, get_address (msg));
2914         } else {
2915                 s = _select;
2916         }
2917         if (s) {
2918                 float abs;
2919                 abs = slider_position_to_gain_with_max (val, 2.0);
2920                 if (s->gain_control()) {
2921                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2922                         return 0;
2923                 }
2924         }
2925         return sel_fail ("fader", 0, get_address (msg));
2926 }
2927
2928 int
2929 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2930 {
2931         if (!session) return -1;
2932         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2933
2934         if (s) {
2935                 if (s->trim_control()) {
2936                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2937                         return 0;
2938                 }
2939
2940         }
2941
2942         return -1;
2943 }
2944
2945 int
2946 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2947 {
2948         int ret;
2949         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2950         if (ret != 0) {
2951                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2952         }
2953
2954 return 0;
2955 }
2956
2957 int
2958 OSC::sel_trim (float val, lo_message msg)
2959 {
2960         OSCSurface *sur = get_surface(get_address (msg));
2961         boost::shared_ptr<Stripable> s;
2962         if (sur->expand_enable) {
2963                 s = get_strip (sur->expand, get_address (msg));
2964         } else {
2965                 s = _select;
2966         }
2967         if (s) {
2968                 if (s->trim_control()) {
2969                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2970                         return 0;
2971                 }
2972         }
2973         return sel_fail ("trimdB", 0, get_address (msg));
2974 }
2975
2976 int
2977 OSC::sel_pan_position (float val, lo_message msg)
2978 {
2979         OSCSurface *sur = get_surface(get_address (msg));
2980         boost::shared_ptr<Stripable> s;
2981         if (sur->expand_enable) {
2982                 s = get_strip (sur->expand, get_address (msg));
2983         } else {
2984                 s = _select;
2985         }
2986         if (s) {
2987                 if(s->pan_azimuth_control()) {
2988                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2989                         return 0;
2990                 }
2991         }
2992         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2993 }
2994
2995 int
2996 OSC::sel_pan_width (float val, lo_message msg)
2997 {
2998         OSCSurface *sur = get_surface(get_address (msg));
2999         boost::shared_ptr<Stripable> s;
3000         if (sur->expand_enable) {
3001                 s = get_strip (sur->expand, get_address (msg));
3002         } else {
3003                 s = _select;
3004         }
3005         if (s) {
3006                 if (s->pan_width_control()) {
3007                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3008                         return 0;
3009                 }
3010         }
3011         return sel_fail ("pan_stereo_width", 1, get_address (msg));
3012 }
3013
3014 int
3015 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
3016 {
3017         if (!session) return -1;
3018         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3019
3020         if (s) {
3021                 if(s->pan_azimuth_control()) {
3022                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
3023                         return 0;
3024                 }
3025         }
3026
3027         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
3028 }
3029
3030 int
3031 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
3032 {
3033         if (!session) return -1;
3034         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3035
3036         if (s) {
3037                 if (s->pan_width_control()) {
3038                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
3039                         return 0;
3040                 }
3041         }
3042
3043         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
3044 }
3045
3046 int
3047 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
3048 {
3049         if (!session) {
3050                 return -1;
3051         }
3052         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3053         float abs;
3054         if (s) {
3055                 if (id > 0) {
3056                         --id;
3057                 }
3058 #ifdef MIXBUS
3059                 abs = val;
3060 #else
3061                 if (val < -192) {
3062                         abs = 0;
3063                 } else {
3064                         abs = dB_to_coefficient (val);
3065                 }
3066 #endif
3067                 if (s->send_level_controllable (id)) {
3068                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3069                         return 0;
3070                 }
3071         }
3072         return 0;
3073 }
3074
3075 int
3076 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
3077 {
3078         if (!session) {
3079                 return -1;
3080         }
3081         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3082         float abs;
3083         if (s) {
3084
3085                 if (id > 0) {
3086                         --id;
3087                 }
3088
3089                 if (s->send_level_controllable (id)) {
3090 #ifdef MIXBUS
3091                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3092 #else
3093                         abs = slider_position_to_gain_with_max (val, 2.0);
3094 #endif
3095                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3096                         return 0;
3097                 }
3098         }
3099         return 0;
3100 }
3101
3102 int
3103 OSC::sel_sendgain (int id, float val, lo_message msg)
3104 {
3105         OSCSurface *sur = get_surface(get_address (msg));
3106         boost::shared_ptr<Stripable> s;
3107         if (sur->expand_enable) {
3108                 s = get_strip (sur->expand, get_address (msg));
3109         } else {
3110                 s = _select;
3111         }
3112         float abs;
3113         if (s) {
3114                 if (id > 0) {
3115                         --id;
3116                 }
3117 #ifdef MIXBUS
3118                 abs = val;
3119 #else
3120                 if (val < -192) {
3121                         abs = 0;
3122                 } else {
3123                         abs = dB_to_coefficient (val);
3124                 }
3125 #endif
3126                 if (s->send_level_controllable (id)) {
3127                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3128                         return 0;
3129                 }
3130         }
3131         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
3132 }
3133
3134 int
3135 OSC::sel_sendfader (int id, float val, lo_message msg)
3136 {
3137         OSCSurface *sur = get_surface(get_address (msg));
3138         boost::shared_ptr<Stripable> s;
3139         if (sur->expand_enable) {
3140                 s = get_strip (sur->expand, get_address (msg));
3141         } else {
3142                 s = _select;
3143         }
3144         float abs;
3145         if (s) {
3146
3147                 if (id > 0) {
3148                         --id;
3149                 }
3150
3151                 if (s->send_level_controllable (id)) {
3152 #ifdef MIXBUS
3153                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3154 #else
3155                         abs = slider_position_to_gain_with_max (val, 2.0);
3156 #endif
3157                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3158                         return 0;
3159                 }
3160         }
3161         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3162 }
3163
3164 int
3165 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3166 {
3167         if (!session) {
3168                 return -1;
3169         }
3170         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3171
3172         if (s) {
3173
3174                 /* revert to zero-based counting */
3175
3176                 if (sid > 0) {
3177                         --sid;
3178                 }
3179
3180                 if (s->send_enable_controllable (sid)) {
3181                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
3182                         return 0;
3183                 }
3184
3185                 if (s->send_level_controllable (sid)) {
3186                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3187                         if (!r) {
3188                                 return 0;
3189                         }
3190                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3191                         if (snd) {
3192                                 if (val) {
3193                                         snd->activate();
3194                                 } else {
3195                                         snd->deactivate();
3196                                 }
3197                         }
3198                         return 0;
3199                 }
3200
3201         }
3202
3203         return -1;
3204 }
3205
3206 int
3207 OSC::sel_sendenable (int id, float val, lo_message msg)
3208 {
3209         OSCSurface *sur = get_surface(get_address (msg));
3210         boost::shared_ptr<Stripable> s;
3211         if (sur->expand_enable) {
3212                 s = get_strip (sur->expand, get_address (msg));
3213         } else {
3214                 s = _select;
3215         }
3216         if (s) {
3217                 if (id > 0) {
3218                         --id;
3219                 }
3220                 if (s->send_enable_controllable (id)) {
3221                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
3222                         return 0;
3223                 }
3224                 if (s->send_level_controllable (id)) {
3225                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3226                         if (!r) {
3227                                 // should never get here
3228                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3229                         }
3230                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
3231                         if (snd) {
3232                                 if (val) {
3233                                         snd->activate();
3234                                 } else {
3235                                         snd->deactivate();
3236                                 }
3237                         }
3238                         return 0;
3239                 }
3240         }
3241         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3242 }
3243
3244 int
3245 OSC::route_plugin_list (int ssid, lo_message msg) {
3246         if (!session) {
3247                 return -1;
3248         }
3249
3250         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3251
3252         if (!r) {
3253                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3254                 return -1;
3255         }
3256         int piid = 0;
3257
3258         lo_message reply = lo_message_new ();
3259         lo_message_add_int32 (reply, ssid);
3260
3261
3262         for (;;) {
3263                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
3264                 if ( !redi ) {
3265                         break;
3266                 }
3267
3268                 boost::shared_ptr<PluginInsert> pi;
3269
3270                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3271                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3272                         continue;
3273                 }
3274                 lo_message_add_int32 (reply, piid + 1);
3275
3276                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3277                 lo_message_add_string (reply, pip->name());
3278
3279                 piid++;
3280         }
3281
3282         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
3283         lo_message_free (reply);
3284         return 0;
3285 }
3286
3287 int
3288 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
3289         if (!session) {
3290                 return -1;
3291         }
3292
3293         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3294
3295         if (!r) {
3296                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3297                 return -1;
3298         }
3299
3300         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3301
3302         if (!redi) {
3303                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3304                 return -1;
3305         }
3306
3307         boost::shared_ptr<PluginInsert> pi;
3308
3309         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3310                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3311                 return -1;
3312         }
3313
3314         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3315         bool ok = false;
3316
3317         lo_message reply = lo_message_new();
3318         lo_message_add_int32 (reply, ssid);
3319         lo_message_add_int32 (reply, piid);
3320         lo_message_add_string (reply, pip->name());
3321         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
3322
3323                 uint32_t controlid = pip->nth_parameter(ppi, ok);
3324                 if (!ok) {
3325                         continue;
3326                 }
3327                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3328                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3329
3330                                 lo_message_add_int32 (reply, ppi + 1);
3331                                 ParameterDescriptor pd;
3332                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
3333                                 lo_message_add_string (reply, pd.label.c_str());
3334
3335                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
3336                                 int flags = 0;
3337                                 flags |= pd.enumeration ? 1 : 0;
3338                                 flags |= pd.integer_step ? 2 : 0;
3339                                 flags |= pd.logarithmic ? 4 : 0;
3340                                 flags |= pd.max_unbound ? 8 : 0;
3341                                 flags |= pd.min_unbound ? 16 : 0;
3342                                 flags |= pd.sr_dependent ? 32 : 0;
3343                                 flags |= pd.toggled ? 64 : 0;
3344                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
3345                                 lo_message_add_int32 (reply, flags);
3346
3347                                 lo_message_add_int32 (reply, pd.datatype);
3348                                 lo_message_add_float (reply, pd.lower);
3349                                 lo_message_add_float (reply, pd.upper);
3350                                 lo_message_add_string (reply, pd.print_fmt.c_str());
3351                                 if ( pd.scale_points ) {
3352                                         lo_message_add_int32 (reply, pd.scale_points->size());
3353                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
3354                                                 lo_message_add_int32 (reply, i->second);
3355                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
3356                                         }
3357                                 }
3358                                 else {
3359                                         lo_message_add_int32 (reply, 0);
3360                                 }
3361                                 if ( c ) {
3362                                         lo_message_add_double (reply, c->get_value());
3363                                 }
3364                                 else {
3365                                         lo_message_add_double (reply, 0);
3366                         }
3367                 }
3368         }
3369
3370         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3371         lo_message_free (reply);
3372
3373         return 0;
3374 }
3375
3376 int
3377 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3378         if (!session) {
3379                 return -1;
3380         }
3381
3382         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3383
3384         if (!r) {
3385                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3386                 return -1;
3387         }
3388
3389         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3390
3391         if (!redi) {
3392                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3393                 return -1;
3394         }
3395
3396         boost::shared_ptr<PluginInsert> pi;
3397
3398         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3399                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3400                 return -1;
3401         }
3402
3403         pi->reset_parameters_to_default ();
3404
3405         return 0;
3406 }
3407
3408 int
3409 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
3410 {
3411         if (!session)
3412                 return -1;
3413         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3414
3415         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3416
3417         if (!r) {
3418                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3419                 return -1;
3420         }
3421
3422         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3423
3424         if (!redi) {
3425                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3426                 return -1;
3427         }
3428
3429         boost::shared_ptr<PluginInsert> pi;
3430
3431         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3432                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3433                 return -1;
3434         }
3435
3436         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3437         bool ok=false;
3438
3439         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3440
3441         if (!ok) {
3442                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3443                 return -1;
3444         }
3445
3446         if (!pip->parameter_is_input(controlid)) {
3447                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3448                 return -1;
3449         }
3450
3451         ParameterDescriptor pd;
3452         pi->plugin()->get_parameter_descriptor (controlid,pd);
3453
3454         if (val >= pd.lower && val <= pd.upper) {
3455
3456                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3457                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3458                 c->set_value (val, PBD::Controllable::NoGroup);
3459         } else {
3460                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3461                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3462         }
3463
3464         return 0;
3465 }
3466
3467 //prints to cerr only
3468 int
3469 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3470 {
3471         if (!session) {
3472                 return -1;
3473         }
3474         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3475
3476         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3477
3478         if (!r) {
3479                 return -1;
3480         }
3481
3482         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3483
3484         if (!redi) {
3485                 return -1;
3486         }
3487
3488         boost::shared_ptr<PluginInsert> pi;
3489
3490         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3491                 return -1;
3492         }
3493
3494         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3495         bool ok=false;
3496
3497         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3498
3499         if (!ok) {
3500                 return -1;
3501         }
3502
3503         ParameterDescriptor pd;
3504
3505         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3506                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3507
3508                 cerr << "parameter:     " << pd.label  << "\n";
3509                 if (c) {
3510                         cerr << "current value: " << c->get_value () << "\n";
3511                 } else {
3512                         cerr << "current value not available, control does not exist\n";
3513                 }
3514                 cerr << "lower value:   " << pd.lower << "\n";
3515                 cerr << "upper value:   " << pd.upper << "\n";
3516         }
3517
3518         return 0;
3519 }
3520
3521 int
3522 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3523 {
3524         if (!session)
3525                 return -1;
3526         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3527
3528         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3529
3530         if (!r) {
3531                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3532                 return -1;
3533         }
3534
3535         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3536
3537         if (!redi) {
3538                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3539                 return -1;
3540         }
3541
3542         boost::shared_ptr<PluginInsert> pi;
3543
3544         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3545                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3546                 return -1;
3547         }
3548
3549         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3550         pi->activate();
3551
3552         return 0;
3553 }
3554
3555 int
3556 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3557 {
3558         if (!session)
3559                 return -1;
3560         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3561
3562         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3563
3564         if (!r) {
3565                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3566                 return -1;
3567         }
3568
3569         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3570
3571         if (!redi) {
3572                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3573                 return -1;
3574         }
3575
3576         boost::shared_ptr<PluginInsert> pi;
3577
3578         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3579                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3580                 return -1;
3581         }
3582
3583         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3584         pi->deactivate();
3585
3586         return 0;
3587 }
3588
3589 // select
3590
3591 int
3592 OSC::sel_pan_elevation (float val, lo_message msg)
3593 {
3594         OSCSurface *sur = get_surface(get_address (msg));
3595         boost::shared_ptr<Stripable> s;
3596         if (sur->expand_enable) {
3597                 s = get_strip (sur->expand, get_address (msg));
3598         } else {
3599                 s = _select;
3600         }
3601         if (s) {
3602                 if (s->pan_elevation_control()) {
3603                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3604                         return 0;
3605                 }
3606         }
3607         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3608 }
3609
3610 int
3611 OSC::sel_pan_frontback (float val, lo_message msg)
3612 {
3613         OSCSurface *sur = get_surface(get_address (msg));
3614         boost::shared_ptr<Stripable> s;
3615         if (sur->expand_enable) {
3616                 s = get_strip (sur->expand, get_address (msg));
3617         } else {
3618                 s = _select;
3619         }
3620         if (s) {
3621                 if (s->pan_frontback_control()) {
3622                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3623                         return 0;
3624                 }
3625         }
3626         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3627 }
3628
3629 int
3630 OSC::sel_pan_lfe (float val, lo_message msg)
3631 {
3632         OSCSurface *sur = get_surface(get_address (msg));
3633         boost::shared_ptr<Stripable> s;
3634         if (sur->expand_enable) {
3635                 s = get_strip (sur->expand, get_address (msg));
3636         } else {
3637                 s = _select;
3638         }
3639         if (s) {
3640                 if (s->pan_lfe_control()) {
3641                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3642                         return 0;
3643                 }
3644         }
3645         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3646 }
3647
3648 // compressor control
3649 int
3650 OSC::sel_comp_enable (float val, lo_message msg)
3651 {
3652         OSCSurface *sur = get_surface(get_address (msg));
3653         boost::shared_ptr<Stripable> s;
3654         if (sur->expand_enable) {
3655                 s = get_strip (sur->expand, get_address (msg));
3656         } else {
3657                 s = _select;
3658         }
3659         if (s) {
3660                 if (s->comp_enable_controllable()) {
3661                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3662                         return 0;
3663                 }
3664         }
3665         return sel_fail ("comp_enable", 0, get_address (msg));
3666 }
3667
3668 int
3669 OSC::sel_comp_threshold (float val, lo_message msg)
3670 {
3671         OSCSurface *sur = get_surface(get_address (msg));
3672         boost::shared_ptr<Stripable> s;
3673         if (sur->expand_enable) {
3674                 s = get_strip (sur->expand, get_address (msg));
3675         } else {
3676                 s = _select;
3677         }
3678         if (s) {
3679                 if (s->comp_threshold_controllable()) {
3680                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3681                         return 0;
3682                 }
3683         }
3684         return sel_fail ("comp_threshold", 0, get_address (msg));
3685 }
3686
3687 int
3688 OSC::sel_comp_speed (float val, lo_message msg)
3689 {
3690         OSCSurface *sur = get_surface(get_address (msg));
3691         boost::shared_ptr<Stripable> s;
3692         if (sur->expand_enable) {
3693                 s = get_strip (sur->expand, get_address (msg));
3694         } else {
3695                 s = _select;
3696         }
3697         if (s) {
3698                 if (s->comp_speed_controllable()) {
3699                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3700                         return 0;
3701                 }
3702         }
3703         return sel_fail ("comp_speed", 0, get_address (msg));
3704 }
3705
3706 int
3707 OSC::sel_comp_mode (float val, lo_message msg)
3708 {
3709         OSCSurface *sur = get_surface(get_address (msg));
3710         boost::shared_ptr<Stripable> s;
3711         if (sur->expand_enable) {
3712                 s = get_strip (sur->expand, get_address (msg));
3713         } else {
3714                 s = _select;
3715         }
3716         if (s) {
3717                 if (s->comp_mode_controllable()) {
3718                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3719                         return 0;
3720                 }
3721         }
3722         return sel_fail ("comp_mode", 0, get_address (msg));
3723 }
3724
3725 int
3726 OSC::sel_comp_makeup (float val, lo_message msg)
3727 {
3728         OSCSurface *sur = get_surface(get_address (msg));
3729         boost::shared_ptr<Stripable> s;
3730         if (sur->expand_enable) {
3731                 s = get_strip (sur->expand, get_address (msg));
3732         } else {
3733                 s = _select;
3734         }
3735         if (s) {
3736                 if (s->comp_makeup_controllable()) {
3737                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3738                         return 0;
3739                 }
3740         }
3741         return sel_fail ("comp_makeup", 0, get_address (msg));
3742 }
3743
3744 // EQ control
3745
3746 int
3747 OSC::sel_eq_enable (float val, lo_message msg)
3748 {
3749         OSCSurface *sur = get_surface(get_address (msg));
3750         boost::shared_ptr<Stripable> s;
3751         if (sur->expand_enable) {
3752                 s = get_strip (sur->expand, get_address (msg));
3753         } else {
3754                 s = _select;
3755         }
3756         if (s) {
3757                 if (s->eq_enable_controllable()) {
3758                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3759                         return 0;
3760                 }
3761         }
3762         return sel_fail ("eq_enable", 0, get_address (msg));
3763 }
3764
3765 int
3766 OSC::sel_eq_hpf (float val, lo_message msg)
3767 {
3768         OSCSurface *sur = get_surface(get_address (msg));
3769         boost::shared_ptr<Stripable> s;
3770         if (sur->expand_enable) {
3771                 s = get_strip (sur->expand, get_address (msg));
3772         } else {
3773                 s = _select;
3774         }
3775         if (s) {
3776                 if (s->filter_freq_controllable(true)) {
3777                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
3778                         return 0;
3779                 }
3780         }
3781         return sel_fail ("eq_hpf", 0, get_address (msg));
3782 }
3783 // need to add two more filter controls
3784
3785 int
3786 OSC::sel_eq_gain (int id, float val, lo_message msg)
3787 {
3788         OSCSurface *sur = get_surface(get_address (msg));
3789         boost::shared_ptr<Stripable> s;
3790         if (sur->expand_enable) {
3791                 s = get_strip (sur->expand, get_address (msg));
3792         } else {
3793                 s = _select;
3794         }
3795         if (s) {
3796                 if (id > 0) {
3797                         --id;
3798                 }
3799                 if (s->eq_gain_controllable (id)) {
3800                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3801                         return 0;
3802                 }
3803         }
3804         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3805 }
3806
3807 int
3808 OSC::sel_eq_freq (int id, float val, lo_message msg)
3809 {
3810         OSCSurface *sur = get_surface(get_address (msg));
3811         boost::shared_ptr<Stripable> s;
3812         if (sur->expand_enable) {
3813                 s = get_strip (sur->expand, get_address (msg));
3814         } else {
3815                 s = _select;
3816         }
3817         if (s) {
3818                 if (id > 0) {
3819                         --id;
3820                 }
3821                 if (s->eq_freq_controllable (id)) {
3822                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3823                         return 0;
3824                 }
3825         }
3826         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3827 }
3828
3829 int
3830 OSC::sel_eq_q (int id, float val, lo_message msg)
3831 {
3832         OSCSurface *sur = get_surface(get_address (msg));
3833         boost::shared_ptr<Stripable> s;
3834         if (sur->expand_enable) {
3835                 s = get_strip (sur->expand, get_address (msg));
3836         } else {
3837                 s = _select;
3838         }
3839         if (s) {
3840                 if (id > 0) {
3841                         --id;
3842                 }
3843                 if (s->eq_q_controllable (id)) {
3844                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3845                         return 0;
3846                 }
3847         }
3848         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3849 }
3850
3851 int
3852 OSC::sel_eq_shape (int id, float val, lo_message msg)
3853 {
3854         OSCSurface *sur = get_surface(get_address (msg));
3855         boost::shared_ptr<Stripable> s;
3856         if (sur->expand_enable) {
3857                 s = get_strip (sur->expand, get_address (msg));
3858         } else {
3859                 s = _select;
3860         }
3861         if (s) {
3862                 if (id > 0) {
3863                         --id;
3864                 }
3865                 if (s->eq_shape_controllable (id)) {
3866                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3867                         return 0;
3868                 }
3869         }
3870         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3871 }
3872
3873 void
3874 OSC::gui_selection_changed ()
3875 {
3876         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3877
3878         if (strip) {
3879                 _select = strip;
3880                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3881                         OSCSurface* sur = &_surface[it];
3882                         if(!sur->expand_enable) {
3883                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3884                                 _strip_select (strip, addr);
3885                         }
3886                 }
3887         }
3888 }
3889
3890 // timer callbacks
3891 bool
3892 OSC::periodic (void)
3893 {
3894         if (!tick) {
3895                 Glib::usleep(100); // let flurry of signals subside
3896                 if (global_init) {
3897                         for (uint32_t it = 0; it < _surface.size(); it++) {
3898                                 OSCSurface* sur = &_surface[it];
3899                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3900                                 global_feedback (sur->feedback, addr, sur->gainmode);
3901                         }
3902                         global_init = false;
3903                         tick = true;
3904                 }
3905                 if (bank_dirty) {
3906                         _recalcbanks ();
3907                         bank_dirty = false;
3908                         tick = true;
3909                 }
3910         }
3911
3912         if (scrub_speed != 0) {
3913                 // for those jog wheels that don't have 0 on release (touch), time out.
3914                 int64_t now = ARDOUR::get_microseconds ();
3915                 int64_t diff = now - scrub_time;
3916                 if (diff > 120000) {
3917                         scrub_speed = 0;
3918                         session->request_transport_speed (0);
3919                         // locate to the place PH was at last tick
3920                         session->request_locate (scrub_place, false);
3921                 }
3922         }
3923
3924         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3925
3926                 OSCGlobalObserver* go;
3927
3928                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3929                         go->tick();
3930                 }
3931         }
3932         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3933
3934                 OSCRouteObserver* ro;
3935
3936                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3937                         ro->tick();
3938                 }
3939         }
3940         for (uint32_t it = 0; it < _surface.size(); it++) {
3941                 OSCSurface* sur = &_surface[it];
3942                 OSCSelectObserver* so;
3943                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3944                         so->tick();
3945                 }
3946         }
3947         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
3948
3949                 OSCCueObserver* co;
3950
3951                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
3952                         co->tick();
3953                 }
3954         }
3955         return true;
3956 }
3957
3958 int
3959 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3960 {
3961         OSCSurface *sur = get_surface(addr);
3962
3963         ostringstream os;
3964         lo_message reply;
3965         if (ssid) {
3966                 reply = lo_message_new ();
3967                 if (sur->feedback[2]) {
3968                         os << "/strip/" << path << "/" << ssid;
3969                 } else {
3970                         os << "/strip/" << path;
3971                         lo_message_add_int32 (reply, ssid);
3972                 }
3973                 string str_pth = os.str();
3974                 lo_message_add_float (reply, (float) val);
3975
3976                 lo_send_message (addr, str_pth.c_str(), reply);
3977                 lo_message_free (reply);
3978         }
3979         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3980                 os.str("");
3981                 os << "/select/" << path;
3982                 string sel_pth = os.str();
3983                 reply = lo_message_new ();
3984                 lo_message_add_float (reply, (float) val);
3985                 lo_send_message (addr, sel_pth.c_str(), reply);
3986                 lo_message_free (reply);
3987         }
3988
3989         return 0;
3990 }
3991
3992 int
3993 OSC::sel_fail (string path, float val, lo_address addr)
3994 {
3995         ostringstream os;
3996         os.str("");
3997         os << "/select/" << path;
3998         string sel_pth = os.str();
3999         lo_message reply = lo_message_new ();
4000         lo_message_add_float (reply, (float) val);
4001         lo_send_message (addr, sel_pth.c_str(), reply);
4002         lo_message_free (reply);
4003
4004         return 0;
4005 }
4006
4007 int
4008 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
4009 {
4010         OSCSurface *sur = get_surface(addr);
4011
4012         ostringstream os;
4013         lo_message reply;
4014         reply = lo_message_new ();
4015         if (sur->feedback[2]) {
4016                 os << "/select/" << path << "/" << id;
4017         } else {
4018                 os << "/select/" << path;
4019                 lo_message_add_int32 (reply, id);
4020         }
4021         string str_pth = os.str();
4022         lo_message_add_float (reply, (float) val);
4023
4024         lo_send_message (addr, str_pth.c_str(), reply);
4025         lo_message_free (reply);
4026
4027         return 0;
4028 }
4029
4030 XMLNode&
4031 OSC::get_state ()
4032 {
4033         XMLNode& node (ControlProtocol::get_state());
4034         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
4035         node.set_property ("address-only", address_only);
4036         node.set_property ("remote-port", remote_port);
4037         node.set_property ("banksize", default_banksize);
4038         node.set_property ("striptypes", default_strip);
4039         node.set_property ("feedback", default_feedback);
4040         node.set_property ("gainmode", default_gainmode);
4041         if (_surface.size()) {
4042                 XMLNode* config = new XMLNode (X_("Configurations"));
4043                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4044                         OSCSurface* sur = &_surface[it];
4045                         XMLNode* devnode = new XMLNode (X_("Configuration"));
4046                         devnode->set_property (X_("url"), sur->remote_url);
4047                         devnode->set_property (X_("bank-size"), sur->bank_size);
4048                         devnode->set_property (X_("strip-types"), (uint64_t)sur->strip_types.to_ulong());
4049                         devnode->set_property (X_("feedback"), (uint64_t)sur->feedback.to_ulong());
4050                         devnode->set_property (X_("gainmode"), sur->gainmode);
4051                         config->add_child_nocopy (*devnode);
4052                 }
4053                 node.add_child_nocopy (*config);
4054         }
4055         return node;
4056 }
4057
4058 int
4059 OSC::set_state (const XMLNode& node, int version)
4060 {
4061         if (ControlProtocol::set_state (node, version)) {
4062                 return -1;
4063         }
4064         int32_t debugmode;
4065         if (node.get_property (X_("debugmode"), debugmode)) {
4066                 _debugmode = OSCDebugMode (debugmode);
4067         }
4068
4069         node.get_property (X_("address-only"), address_only);
4070         node.get_property (X_("remote-port"), remote_port);
4071         node.get_property (X_("banksize"), default_banksize);
4072         node.get_property (X_("striptypes"), default_strip);
4073         node.get_property (X_("feedback"), default_feedback);
4074         node.get_property (X_("gainmode"), default_gainmode);
4075
4076         XMLNode* cnode = node.child (X_("Configurations"));
4077
4078         if (cnode) {
4079                 XMLNodeList const& devices = cnode->children();
4080                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
4081                         OSCSurface s;
4082                         if (!(*d)->get_property (X_("url"), s.remote_url)) {
4083                                 continue;
4084                         }
4085
4086                         bank_dirty = true;
4087
4088                         (*d)->get_property (X_("bank-size"), s.bank_size);
4089
4090                         uint64_t bits;
4091                         if ((*d)->get_property (X_ ("strip-types"), bits)) {
4092                                 s.strip_types = bits;
4093                         }
4094                         if ((*d)->get_property (X_("feedback"), bits)) {
4095                                 s.feedback = bits;
4096                         }
4097                         (*d)->get_property (X_("gainmode"), s.gainmode);
4098
4099                         s.bank = 1;
4100                         s.sel_obs = 0;
4101                         s.expand = 0;
4102                         s.expand_enable = false;
4103                         s.strips = get_sorted_stripables (s.strip_types, s.cue);
4104                         s.nstrips = s.strips.size ();
4105                         s.no_clear = false;
4106                         s.jogmode = JOG;
4107                         s.cue = false;
4108                         s.aux = 0;
4109                         _surface.push_back (s);
4110                 }
4111         }
4112         global_init = true;
4113         tick = false;
4114
4115         return 0;
4116 }
4117
4118 // predicate for sort call in get_sorted_stripables
4119 struct StripableByPresentationOrder
4120 {
4121         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4122         {
4123                 return a->presentation_info().order() < b->presentation_info().order();
4124         }
4125
4126         bool operator () (const Stripable & a, const Stripable & b) const
4127         {
4128                 return a.presentation_info().order() < b.presentation_info().order();
4129         }
4130
4131         bool operator () (const Stripable * a, const Stripable * b) const
4132         {
4133                 return a->presentation_info().order() < b->presentation_info().order();
4134         }
4135 };
4136
4137 OSC::Sorted
4138 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4139 {
4140         Sorted sorted;
4141
4142         // fetch all stripables
4143         StripableList stripables;
4144
4145         session->get_stripables (stripables);
4146
4147         // Look for stripables that match bit in sur->strip_types
4148         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4149
4150                 boost::shared_ptr<Stripable> s = *it;
4151                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4152                         // do nothing... skip it
4153                 } else {
4154
4155                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
4156                                 sorted.push_back (s);
4157                         } else
4158                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
4159                                 sorted.push_back (s);
4160                         } else
4161                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
4162                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4163                                 // r->feeds (session->master_out()) may make more sense
4164                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
4165                                         // this is a bus
4166                                         if (types[2]) {
4167                                                 sorted.push_back (s);
4168                                         }
4169                                 } else {
4170                                         // this is an Aux out
4171                                         if (types[7]) {
4172                                                 sorted.push_back (s);
4173                                         }
4174                                 }
4175                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
4176                                 sorted.push_back (s);
4177                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
4178                                 sorted.push_back (s);
4179                         } else if (types[8] && (s->is_selected())) {
4180                                 sorted.push_back (s);
4181                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4182                                 sorted.push_back (s);
4183                         }
4184                 }
4185         }
4186         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4187         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
4188         if (types[5]) {
4189                 sorted.push_back (session->master_out());
4190         }
4191         if (types[6]) {
4192                 sorted.push_back (session->monitor_out());
4193         }
4194         return sorted;
4195 }
4196
4197 int
4198 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
4199 {
4200         int ret = 1; /* unhandled */
4201
4202         if (!strncmp (path, "/cue/aux", 8)) {
4203                 // set our Aux bus
4204                 ret = cue_set (argv[0]->i, msg);
4205         }
4206         else if (!strncmp (path, "/cue/connect", 12)) {
4207                 // Connect to default Aux bus
4208                 if ((!argc) || argv[0]->i) {
4209                         ret = cue_set (1, msg);
4210                 }
4211         }
4212         else if (!strncmp (path, "/cue/next_aux", 13)) {
4213                 // switch to next Aux bus
4214                 if ((!argc) || argv[0]->i) {
4215                         ret = cue_next (msg);
4216                 }
4217         }
4218         else if (!strncmp (path, "/cue/previous_aux", 17)) {
4219                 // switch to previous Aux bus
4220                 if ((!argc) || argv[0]->i) {
4221                         ret = cue_previous (msg);
4222                 }
4223         }
4224         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
4225                 int id = atoi (&path[16]);
4226                 ret = cue_send_fader (id, argv[0]->f, msg);
4227         }
4228         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
4229                 int id = atoi (&path[17]);
4230                 ret = cue_send_enable (id, argv[0]->f, msg);
4231         }
4232         else if (!strncmp (path, "/cue/fader", 10)) {
4233                 ret = cue_aux_fader (argv[0]->f, msg);
4234         }
4235         else if (!strncmp (path, "/cue/mute", 9)) {
4236                 ret = cue_aux_mute (argv[0]->f, msg);
4237         }
4238
4239         return ret;
4240 }
4241
4242 int
4243 OSC::cue_set (uint32_t aux, lo_message msg)
4244 {
4245         return _cue_set (aux, get_address (msg));
4246 }
4247
4248 int
4249 OSC::_cue_set (uint32_t aux, lo_address addr)
4250 {
4251         int ret = 1;
4252         OSCSurface *s = get_surface(addr);
4253         s->bank_size = 0;
4254         s->strip_types = 128;
4255         s->feedback = 0;
4256         s->gainmode = 1;
4257         s->cue = true;
4258         s->strips = get_sorted_stripables(s->strip_types, s->cue);
4259
4260         s->nstrips = s->strips.size();
4261
4262         if (aux < 1) {
4263                 aux = 1;
4264         } else if (aux > s->nstrips) {
4265                 aux = s->nstrips;
4266         }
4267         s->aux = aux;
4268
4269         // get rid of any old CueObsevers for this address
4270         //cueobserver_connections.drop_connections ();
4271         CueObservers::iterator x;
4272         for (x = cue_observers.begin(); x != cue_observers.end();) {
4273
4274                 OSCCueObserver* co;
4275
4276                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4277
4278                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
4279
4280                         if (res == 0) {
4281                                 delete *x;
4282                                 x = cue_observers.erase (x);
4283                         } else {
4284                                 ++x;
4285                         }
4286                 } else {
4287                         ++x;
4288                 }
4289         }
4290
4291         // get a list of Auxes
4292         for (uint32_t n = 0; n < s->nstrips; ++n) {
4293                 boost::shared_ptr<Stripable> stp = s->strips[n];
4294                 if (stp) {
4295                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
4296                         if (aux == n+1) {
4297                                 // aux must be at least one
4298                                 // need a signal if aux vanishes
4299                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
4300
4301                                 // make a list of stripables with sends that go to this bus
4302                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
4303                                 // start cue observer
4304                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
4305                                 cue_observers.push_back (co);
4306                                 ret = 0;
4307                         }
4308
4309                 }
4310         }
4311
4312         return ret;
4313 }
4314
4315 int
4316 OSC::cue_next (lo_message msg)
4317 {
4318         OSCSurface *s = get_surface(get_address (msg));
4319         int ret = 1;
4320
4321         if (!s->cue) {
4322                 ret = cue_set (1, msg);
4323         }
4324         if (s->aux < s->nstrips) {
4325                 ret = cue_set (s->aux + 1, msg);
4326         } else {
4327                 ret = cue_set (s->nstrips, msg);
4328         }
4329         return ret;
4330 }
4331
4332 int
4333 OSC::cue_previous (lo_message msg)
4334 {
4335         OSCSurface *s = get_surface(get_address (msg));
4336         int ret = 1;
4337         if (!s->cue) {
4338                 ret = cue_set (1, msg);
4339         }
4340         if (s->aux > 1) {
4341                 ret = cue_set (s->aux - 1, msg);
4342         }
4343         return ret;
4344 }
4345
4346 boost::shared_ptr<Send>
4347 OSC::cue_get_send (uint32_t id, lo_address addr)
4348 {
4349         OSCSurface *s = get_surface(addr);
4350         if (id && s->aux > 0 && id <= s->sends.size()) {
4351                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
4352                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
4353                 if (r && aux) {
4354                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4355                 }
4356         }
4357         return boost::shared_ptr<Send>();
4358
4359 }
4360
4361 int
4362 OSC::cue_aux_fader (float position, lo_message msg)
4363 {
4364         if (!session) return -1;
4365
4366         OSCSurface *sur = get_surface(get_address (msg));
4367         if (sur->cue) {
4368                 if (sur->aux) {
4369                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4370
4371                         if (s) {
4372                                 float abs;
4373                                 abs = slider_position_to_gain_with_max (position, 2.0);
4374                                 if (s->gain_control()) {
4375                                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4376                                         return 0;
4377                                 }
4378                         }
4379                 }
4380         }
4381         cue_float_message ("/cue/fader", 0, get_address (msg));
4382         return -1;
4383 }
4384
4385 int
4386 OSC::cue_aux_mute (float state, lo_message msg)
4387 {
4388         if (!session) return -1;
4389
4390         OSCSurface *sur = get_surface(get_address (msg));
4391         if (sur->cue) {
4392                 if (sur->aux) {
4393                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4394                         if (s) {
4395                                 if (s->mute_control()) {
4396                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
4397                                         return 0;
4398                                 }
4399                         }
4400                 }
4401         }
4402         cue_float_message ("/cue/mute", 0, get_address (msg));
4403         return -1;
4404 }
4405
4406 int
4407 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
4408 {
4409         if (!session) {
4410                 return -1;
4411         }
4412         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4413         float abs;
4414         if (s) {
4415                 if (s->gain_control()) {
4416                         abs = slider_position_to_gain_with_max (val, 2.0);
4417                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4418                         return 0;
4419                 }
4420         }
4421         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
4422         return -1;
4423 }
4424
4425 int
4426 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
4427 {
4428         if (!session)
4429                 return -1;
4430         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4431         if (s) {
4432                 if (state) {
4433                         s->activate ();
4434                 } else {
4435                         s->deactivate ();
4436                 }
4437                 return 0;
4438         }
4439         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
4440         return -1;
4441 }
4442
4443 int
4444 OSC::cue_float_message (string path, float val, lo_address addr)
4445 {
4446
4447         lo_message reply;
4448         reply = lo_message_new ();
4449         lo_message_add_float (reply, (float) val);
4450
4451         lo_send_message (addr, path.c_str(), reply);
4452         lo_message_free (reply);
4453
4454         return 0;
4455 }
4456
4457 int
4458 OSC::text_message (string path, string val, lo_address addr)
4459 {
4460
4461         lo_message reply;
4462         reply = lo_message_new ();
4463         lo_message_add_string (reply, val.c_str());
4464
4465         lo_send_message (addr, path.c_str(), reply);
4466         lo_message_free (reply);
4467
4468         return 0;
4469 }
4470
4471
4472 // we have to have a sorted list of stripables that have sends pointed at our aux
4473 // we can use the one in osc.cc to get an aux list
4474 OSC::Sorted
4475 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
4476 {
4477         Sorted sorted;
4478         cueobserver_connections.drop_connections ();
4479         // fetch all stripables
4480         StripableList stripables;
4481
4482         session->get_stripables (stripables);
4483
4484         // Look for stripables that have a send to aux
4485         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4486
4487                 boost::shared_ptr<Stripable> s = *it;
4488                 // we only want routes
4489                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4490                 if (r) {
4491                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
4492                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4493                         if (snd) { // test for send to aux
4494                                 sorted.push_back (s);
4495                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
4496                         }
4497                 }
4498
4499
4500         }
4501         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4502
4503         return sorted;
4504 }
4505