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