1ca0dff146b622cbd7758d5176808389b1207e0a
[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_sample);
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_sample());
1191                 }
1192
1193         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
1194
1195                 if (session) {
1196                         lo_send (addr, retpath, "i", session->transport_sample());
1197                 }
1198
1199         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
1200
1201                 if (session) {
1202                         lo_send (addr, retpath, "i", session->transport_sample());
1203                 }
1204
1205         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
1206
1207                 if (session) {
1208                         lo_send (addr, retpath, "i", session->transport_sample());
1209                 }
1210
1211         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
1212
1213                 if (session) {
1214                         lo_send (addr, retpath, "i", session->transport_sample());
1215                 }
1216
1217         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
1218
1219                 if (session) {
1220                         lo_send (addr, retpath, "i", session->transport_sample());
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->sample_rate());
1317         lo_message_add_int64 (reply, session->current_end_sample());
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_sample (lo_message msg)
2172 {
2173         if (!session) {
2174                 return;
2175         }
2176         check_surface (msg);
2177         samplepos_t pos = session->transport_sample ();
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_sample ();
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 (9.9);
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.1));
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         if (get_transport_speed () != 1.0) {
2352                 set_transport_speed (0);
2353         }
2354
2355         switch((uint32_t)mode)
2356         {
2357                 case JOG  :
2358                         text_message ("/jog/mode/name", "Jog", get_address (msg));
2359                         s->jogmode = JOG;
2360                         break;
2361                 case SCRUB:
2362                         text_message ("/jog/mode/name", "Scrub", get_address (msg));
2363                         s->jogmode = SCRUB;
2364                         break;
2365                 case SHUTTLE:
2366                         text_message ("/jog/mode/name", "Shuttle", get_address (msg));
2367                         s->jogmode = SHUTTLE;
2368                         break;
2369                 case SCROLL:
2370                         text_message ("/jog/mode/name", "Scroll", get_address (msg));
2371                         s->jogmode = SCROLL;
2372                         break;
2373                 case TRACK:
2374                         text_message ("/jog/mode/name", "Track", get_address (msg));
2375                         s->jogmode = TRACK;
2376                         break;
2377                 case BANK:
2378                         text_message ("/jog/mode/name", "Bank", get_address (msg));
2379                         s->jogmode = BANK;
2380                         break;
2381                 case NUDGE:
2382                         text_message ("/jog/mode/name", "Nudge", get_address (msg));
2383                         s->jogmode = NUDGE;
2384                         break;
2385                 case MARKER:
2386                         text_message ("/jog/mode/name", "Marker", get_address (msg));
2387                         s->jogmode = MARKER;
2388                         break;
2389                 default:
2390                         PBD::warning << "Jog Mode: " << mode << " is not valid." << endmsg;
2391                         break;
2392         lo_message reply = lo_message_new ();
2393         lo_message_add_int32 (reply, s->jogmode);
2394         lo_send_message (get_address(msg), "/jog/mode", reply);
2395         lo_message_free (reply);
2396
2397         }
2398         return 0;
2399
2400 }
2401
2402 // master and monitor calls
2403 int
2404 OSC::master_set_gain (float dB)
2405 {
2406         if (!session) return -1;
2407         boost::shared_ptr<Stripable> s = session->master_out();
2408         if (s) {
2409                 if (dB < -192) {
2410                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2411                 } else {
2412                         float abs = dB_to_coefficient (dB);
2413                         float top = s->gain_control()->upper();
2414                         if (abs > top) {
2415                                 abs = top;
2416                         }
2417                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2418                 }
2419         }
2420         return 0;
2421 }
2422
2423 int
2424 OSC::master_delta_gain (float delta)
2425 {
2426         if (!session) return -1;
2427         boost::shared_ptr<Stripable> s = session->master_out();
2428         if (s) {
2429                 float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
2430                 if (dB < -192) {
2431                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2432                 } else {
2433                         float abs = dB_to_coefficient (dB);
2434                         float top = s->gain_control()->upper();
2435                         if (abs > top) {
2436                                 abs = top;
2437                         }
2438                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2439                 }
2440         }
2441         return 0;
2442 }
2443
2444 int
2445 OSC::master_set_fader (float position)
2446 {
2447         if (!session) return -1;
2448         boost::shared_ptr<Stripable> s = session->master_out();
2449         if (s) {
2450                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2451         }
2452         return 0;
2453 }
2454
2455 int
2456 OSC::master_set_trim (float dB)
2457 {
2458         if (!session) return -1;
2459         boost::shared_ptr<Stripable> s = session->master_out();
2460
2461         if (s) {
2462                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2463         }
2464
2465         return 0;
2466 }
2467
2468 int
2469 OSC::master_set_pan_stereo_position (float position, lo_message msg)
2470 {
2471         if (!session) return -1;
2472         OSCSurface *sur = get_surface(get_address (msg));
2473
2474         float endposition = .5;
2475         boost::shared_ptr<Stripable> s = session->master_out();
2476
2477         if (s) {
2478                 if (s->pan_azimuth_control()) {
2479                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2480                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
2481                 }
2482         }
2483
2484         if (sur->feedback[4]) {
2485                 lo_message reply = lo_message_new ();
2486                 lo_message_add_float (reply, endposition);
2487
2488                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
2489                 lo_message_free (reply);
2490         }
2491
2492         return 0;
2493 }
2494
2495 int
2496 OSC::master_set_mute (uint32_t state)
2497 {
2498         if (!session) return -1;
2499
2500         boost::shared_ptr<Stripable> s = session->master_out();
2501
2502         if (s) {
2503                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
2504         }
2505
2506         return 0;
2507 }
2508
2509 int
2510 OSC::master_select (lo_message msg)
2511 {
2512         if (!session) {
2513                 return -1;
2514         }
2515         OSCSurface *sur = get_surface(get_address (msg));
2516         sur->expand_enable = false;
2517         boost::shared_ptr<Stripable> s = session->master_out();
2518         if (s) {
2519                 SetStripableSelection (s);
2520         }
2521
2522         return 0;
2523 }
2524
2525 int
2526 OSC::monitor_set_gain (float dB)
2527 {
2528         if (!session) return -1;
2529         boost::shared_ptr<Stripable> s = session->monitor_out();
2530
2531         if (s) {
2532                 if (dB < -192) {
2533                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2534                 } else {
2535                         float abs = dB_to_coefficient (dB);
2536                         float top = s->gain_control()->upper();
2537                         if (abs > top) {
2538                                 abs = top;
2539                         }
2540                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2541                 }
2542         }
2543         return 0;
2544 }
2545
2546 int
2547 OSC::monitor_delta_gain (float delta)
2548 {
2549         if (!session) return -1;
2550         boost::shared_ptr<Stripable> s = session->monitor_out();
2551         if (s) {
2552                 float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
2553                 if (dB < -192) {
2554                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2555                 } else {
2556                         float abs = dB_to_coefficient (dB);
2557                         float top = s->gain_control()->upper();
2558                         if (abs > top) {
2559                                 abs = top;
2560                         }
2561                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2562                 }
2563         }
2564         return 0;
2565 }
2566
2567 int
2568 OSC::monitor_set_fader (float position)
2569 {
2570         if (!session) return -1;
2571         boost::shared_ptr<Stripable> s = session->monitor_out();
2572         if (s) {
2573                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2574         }
2575         return 0;
2576 }
2577
2578 int
2579 OSC::monitor_set_mute (uint32_t state)
2580 {
2581         if (!session) return -1;
2582
2583         if (session->monitor_out()) {
2584                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2585                 mon->set_cut_all (state);
2586         }
2587         return 0;
2588 }
2589
2590 int
2591 OSC::monitor_set_dim (uint32_t state)
2592 {
2593         if (!session) return -1;
2594
2595         if (session->monitor_out()) {
2596                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2597                 mon->set_dim_all (state);
2598         }
2599         return 0;
2600 }
2601
2602 int
2603 OSC::monitor_set_mono (uint32_t state)
2604 {
2605         if (!session) return -1;
2606
2607         if (session->monitor_out()) {
2608                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2609                 mon->set_mono (state);
2610         }
2611         return 0;
2612 }
2613
2614 int
2615 OSC::route_get_sends(lo_message msg) {
2616         if (!session) {
2617                 return -1;
2618         }
2619
2620         lo_arg **argv = lo_message_get_argv(msg);
2621
2622         int rid = argv[0]->i;
2623
2624         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2625         if (!strip) {
2626                 return -1;
2627         }
2628
2629         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2630         if (!r) {
2631                 return -1;
2632         }
2633
2634         lo_message reply = lo_message_new();
2635         lo_message_add_int32(reply, rid);
2636
2637         int i = 0;
2638         for (;;) {
2639                 boost::shared_ptr<Processor> p = r->nth_send(i++);
2640
2641                 if (!p) {
2642                         break;
2643                 }
2644
2645                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2646                 if (isend) {
2647                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
2648                         lo_message_add_string(reply, isend->name().c_str());
2649                         lo_message_add_int32(reply, i);
2650                         boost::shared_ptr<Amp> a = isend->amp();
2651                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
2652                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2653                 }
2654         }
2655         // if used dedicated message path to identify this reply in async operation.
2656         // Naming it #reply wont help the client to identify the content.
2657         lo_send_message(get_address (msg), "/strip/sends", reply);
2658
2659         lo_message_free(reply);
2660
2661         return 0;
2662 }
2663
2664 int
2665 OSC::route_get_receives(lo_message msg) {
2666         if (!session) {
2667                 return -1;
2668         }
2669
2670         lo_arg **argv = lo_message_get_argv(msg);
2671
2672         uint32_t rid = argv[0]->i;
2673
2674
2675         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2676         if (!strip) {
2677                 return -1;
2678         }
2679
2680         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2681         if (!r) {
2682                 return -1;
2683         }
2684
2685         boost::shared_ptr<RouteList> route_list = session->get_routes();
2686
2687         lo_message reply = lo_message_new();
2688
2689         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
2690                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
2691                 if (!tr) {
2692                         continue;
2693                 }
2694                 int j = 0;
2695
2696                 for (;;) {
2697                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
2698
2699                         if (!p) {
2700                                 break;
2701                         }
2702
2703                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2704                         if (isend) {
2705                                 if( isend->target_route()->id() == r->id()){
2706                                         boost::shared_ptr<Amp> a = isend->amp();
2707
2708                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
2709                                         lo_message_add_string(reply, tr->name().c_str());
2710                                         lo_message_add_int32(reply, j);
2711                                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
2712                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2713                                 }
2714                         }
2715                 }
2716         }
2717
2718         // I have used a dedicated message path to identify this reply in async operation.
2719         // Naming it #reply wont help the client to identify the content.
2720         lo_send_message(get_address (msg), "/strip/receives", reply);
2721         lo_message_free(reply);
2722         return 0;
2723 }
2724
2725 // strip calls
2726
2727 int
2728 OSC::set_automation (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
2729 {
2730         if (!session) return -1;
2731
2732         int ret = 1;
2733         OSCSurface *sur = get_surface(get_address (msg));
2734         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2735         uint32_t ctr = 0;
2736         uint32_t aut = 0;
2737         uint32_t ssid;
2738
2739         if (argc) {
2740                 if (types[argc - 1] == 'f') {
2741                         aut = (int)argv[argc - 1]->f;
2742                 } else {
2743                         aut = argv[argc - 1]->i;
2744                 }
2745         }
2746
2747         //parse path first to find stripable
2748         if (!strncmp (path, "/strip/", 7)) {
2749                 // find ssid and stripable
2750                 if (argc > 1) {
2751                         if (types[1] == 'f') {
2752                                 ssid = (uint32_t)argv[0]->f;
2753                         } else {
2754                                 ssid = argv[0]->i;
2755                         }
2756                         strp = get_strip (ssid, get_address (msg));
2757                 } else {
2758                         ssid = atoi (&(strrchr (path, '/' ))[1]);
2759                         strp = get_strip (ssid, get_address (msg));
2760                 }
2761                 ctr = 7;
2762         } else if (!strncmp (path, "/select/", 8)) {
2763                 if (sur->expand_enable && sur->expand) {
2764                         strp = get_strip (sur->expand, get_address (msg));
2765                 } else {
2766                         strp = ControlProtocol::first_selected_stripable();
2767                 }
2768                 ctr = 8;
2769         } else {
2770                 return ret;
2771         }
2772         if (strp) {
2773                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
2774                 // other automatable controls can be added by repeating the next 6.5 lines
2775                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2776                         if (strp->gain_control ()) {
2777                                 control = strp->gain_control ();
2778                         } else {
2779                                 PBD::warning << "No fader for this strip" << endmsg;
2780                         }
2781                 } else {
2782                         PBD::warning << "Automation not available for " << path << endmsg;
2783                 }
2784
2785                 if (control) {
2786
2787                         switch (aut) {
2788                                 case 0:
2789                                         control->set_automation_state (ARDOUR::Off);
2790                                         ret = 0;
2791                                         break;
2792                                 case 1:
2793                                         control->set_automation_state (ARDOUR::Play);
2794                                         ret = 0;
2795                                         break;
2796                                 case 2:
2797                                         control->set_automation_state (ARDOUR::Write);
2798                                         ret = 0;
2799                                         break;
2800                                 case 3:
2801                                         control->set_automation_state (ARDOUR::Touch);
2802                                         ret = 0;
2803                                         break;
2804                                 default:
2805                                         break;
2806                         }
2807                 }
2808         }
2809
2810         return ret;
2811 }
2812
2813 int
2814 OSC::touch_detect (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
2815 {
2816         if (!session) return -1;
2817
2818         int ret = 1;
2819         OSCSurface *sur = get_surface(get_address (msg));
2820         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2821         uint32_t ctr = 0;
2822         uint32_t touch = 0;
2823         uint32_t ssid;
2824
2825         if (argc) {
2826                 if (types[argc - 1] == 'f') {
2827                         touch = (int)argv[argc - 1]->f;
2828                 } else {
2829                         touch = argv[argc - 1]->i;
2830                 }
2831         }
2832
2833         //parse path first to find stripable
2834         if (!strncmp (path, "/strip/", 7)) {
2835                 // find ssid and stripable
2836                 if (argc > 1) {
2837                         if (types[0] == 'f') {
2838                                 ssid = (uint32_t)argv[0]->f;
2839                         } else {
2840                                 ssid = argv[0]->i;
2841                         }
2842                         strp = get_strip (ssid, get_address (msg));
2843                 } else {
2844                         ssid = atoi (&(strrchr (path, '/' ))[1]);
2845                         strp = get_strip (ssid, get_address (msg));
2846                 }
2847                 ctr = 7;
2848         } else if (!strncmp (path, "/select/", 8)) {
2849                 if (sur->expand_enable && sur->expand) {
2850                         strp = get_strip (sur->expand, get_address (msg));
2851                 } else {
2852                         strp = ControlProtocol::first_selected_stripable();
2853                 }
2854                 ctr = 8;
2855         } else {
2856                 return ret;
2857         }
2858         if (strp) {
2859                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
2860                 // other automatable controls can be added by repeating the next 6.5 lines
2861                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2862                         if (strp->gain_control ()) {
2863                                 control = strp->gain_control ();
2864                         } else {
2865                                 PBD::warning << "No fader for this strip" << endmsg;
2866                         }
2867                 } else {
2868                         PBD::warning << "Automation not available for " << path << endmsg;
2869                 }
2870
2871                 if (control) {
2872                         if (touch) {
2873                                 //start touch
2874                                 control->start_touch (control->session().transport_sample());
2875                                 ret = 0;
2876                         } else {
2877                                 // end touch
2878                                 control->stop_touch (control->session().transport_sample());
2879                                 ret = 0;
2880                         }
2881                         // just in case some crazy surface starts sending control values before touch
2882                         FakeTouchMap::iterator x = _touch_timeout.find(control);
2883                         if (x != _touch_timeout.end()) {
2884                                 _touch_timeout.erase (x);
2885                         }
2886                 }
2887         }
2888
2889         return ret;
2890 }
2891
2892 int
2893 OSC::fake_touch (boost::shared_ptr<ARDOUR::AutomationControl> ctrl)
2894 {
2895         if (ctrl) {
2896                 //start touch
2897                 if (ctrl->automation_state() == Touch && !ctrl->touching ()) {
2898                 ctrl->start_touch (ctrl->session().transport_sample());
2899                 _touch_timeout[ctrl] = 10;
2900                 }
2901         }
2902
2903         return 0;
2904 }
2905
2906 int
2907 OSC::route_mute (int ssid, int yn, lo_message msg)
2908 {
2909         if (!session) return -1;
2910         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2911         OSCSurface *sur = get_surface(get_address (msg));
2912
2913         if (s) {
2914                 if (s->mute_control()) {
2915                         s->mute_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
2916                         return 0;
2917                 }
2918         }
2919
2920         return route_send_fail ("mute", ssid, 0, get_address (msg));
2921 }
2922
2923 int
2924 OSC::sel_mute (uint32_t yn, lo_message msg)
2925 {
2926         OSCSurface *sur = get_surface(get_address (msg));
2927         boost::shared_ptr<Stripable> s;
2928         if (sur->expand_enable) {
2929                 s = get_strip (sur->expand, get_address (msg));
2930         } else {
2931                 s = _select;
2932         }
2933         if (s) {
2934                 if (s->mute_control()) {
2935                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2936                         return 0;
2937                 }
2938         }
2939         return sel_fail ("mute", 0, get_address (msg));
2940 }
2941
2942 int
2943 OSC::route_solo (int ssid, int yn, lo_message msg)
2944 {
2945         if (!session) return -1;
2946         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2947         OSCSurface *sur = get_surface(get_address (msg));
2948
2949         if (s) {
2950                 if (s->solo_control()) {
2951                         s->solo_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
2952                 }
2953         }
2954
2955         return route_send_fail ("solo", ssid, 0, get_address (msg));
2956 }
2957
2958 int
2959 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2960 {
2961         if (!session) return -1;
2962         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2963         OSCSurface *sur = get_surface(get_address (msg));
2964
2965         if (s) {
2966                 if (s->solo_isolate_control()) {
2967                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
2968                         return 0;
2969                 }
2970         }
2971
2972         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2973 }
2974
2975 int
2976 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2977 {
2978         if (!session) return -1;
2979         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2980         OSCSurface *sur = get_surface(get_address (msg));
2981
2982         if (s) {
2983                 if (s->solo_safe_control()) {
2984                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
2985                         return 0;
2986                 }
2987         }
2988
2989         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2990 }
2991
2992 int
2993 OSC::sel_solo (uint32_t yn, lo_message msg)
2994 {
2995         OSCSurface *sur = get_surface(get_address (msg));
2996         boost::shared_ptr<Stripable> s;
2997         if (sur->expand_enable) {
2998                 s = get_strip (sur->expand, get_address (msg));
2999         } else {
3000                 s = _select;
3001         }
3002         if (s) {
3003                 if (s->solo_control()) {
3004                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3005                 }
3006         }
3007         return sel_fail ("solo", 0, get_address (msg));
3008 }
3009
3010 int
3011 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
3012 {
3013         OSCSurface *sur = get_surface(get_address (msg));
3014         boost::shared_ptr<Stripable> s;
3015         if (sur->expand_enable) {
3016                 s = get_strip (sur->expand, get_address (msg));
3017         } else {
3018                 s = _select;
3019         }
3020         if (s) {
3021                 if (s->solo_isolate_control()) {
3022                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3023                         return 0;
3024                 }
3025         }
3026         return sel_fail ("solo_iso", 0, get_address (msg));
3027 }
3028
3029 int
3030 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
3031 {
3032         OSCSurface *sur = get_surface(get_address (msg));
3033         boost::shared_ptr<Stripable> s;
3034         if (sur->expand_enable) {
3035                 s = get_strip (sur->expand, get_address (msg));
3036         } else {
3037                 s = _select;
3038         }
3039         if (s) {
3040                 if (s->solo_safe_control()) {
3041                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3042                         return 0;
3043                 }
3044         }
3045         return sel_fail ("solo_safe", 0, get_address (msg));
3046 }
3047
3048 int
3049 OSC::sel_recenable (uint32_t yn, lo_message msg)
3050 {
3051         OSCSurface *sur = get_surface(get_address (msg));
3052         boost::shared_ptr<Stripable> s;
3053         if (sur->expand_enable) {
3054                 s = get_strip (sur->expand, get_address (msg));
3055         } else {
3056                 s = _select;
3057         }
3058         if (s) {
3059                 if (s->rec_enable_control()) {
3060                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3061                         if (s->rec_enable_control()->get_value()) {
3062                                 return 0;
3063                         }
3064                 }
3065         }
3066         return sel_fail ("recenable", 0, get_address (msg));
3067 }
3068
3069 int
3070 OSC::route_recenable (int ssid, int yn, lo_message msg)
3071 {
3072         if (!session) return -1;
3073         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3074         OSCSurface *sur = get_surface(get_address (msg));
3075
3076         if (s) {
3077                 if (s->rec_enable_control()) {
3078                         s->rec_enable_control()->set_value (yn, sur->usegroup);
3079                         if (s->rec_enable_control()->get_value()) {
3080                                 return 0;
3081                         }
3082                 }
3083         }
3084         return route_send_fail ("recenable", ssid, 0, get_address (msg));
3085 }
3086
3087 int
3088 OSC::route_rename(int ssid, char *newname, lo_message msg) {
3089     if (!session) {
3090         return -1;
3091     }
3092
3093     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
3094
3095     if (s) {
3096         s->set_name(std::string(newname));
3097     }
3098
3099     return 0;
3100 }
3101
3102 int
3103 OSC::sel_recsafe (uint32_t yn, lo_message msg)
3104 {
3105         OSCSurface *sur = get_surface(get_address (msg));
3106         boost::shared_ptr<Stripable> s;
3107         if (sur->expand_enable) {
3108                 s = get_strip (sur->expand, get_address (msg));
3109         } else {
3110                 s = _select;
3111         }
3112         if (s) {
3113                 if (s->rec_safe_control()) {
3114                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3115                         if (s->rec_safe_control()->get_value()) {
3116                                 return 0;
3117                         }
3118                 }
3119         }
3120         return sel_fail ("record_safe", 0, get_address (msg));
3121 }
3122
3123 int
3124 OSC::route_recsafe (int ssid, int yn, lo_message msg)
3125 {
3126         if (!session) return -1;
3127         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3128         OSCSurface *sur = get_surface(get_address (msg));
3129         if (s) {
3130                 if (s->rec_safe_control()) {
3131                         s->rec_safe_control()->set_value (yn, sur->usegroup);
3132                         if (s->rec_safe_control()->get_value()) {
3133                                 return 0;
3134                         }
3135                 }
3136         }
3137         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
3138 }
3139
3140 int
3141 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
3142 {
3143         if (!session) return -1;
3144         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3145         OSCSurface *sur = get_surface(get_address (msg));
3146
3147         if (s) {
3148                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3149                 if (track) {
3150                         if (track->monitoring_control()) {
3151                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3152                                 return 0;
3153                         }
3154                 }
3155         }
3156
3157         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
3158 }
3159
3160 int
3161 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
3162 {
3163         OSCSurface *sur = get_surface(get_address (msg));
3164         boost::shared_ptr<Stripable> s;
3165         if (sur->expand_enable) {
3166                 s = get_strip (sur->expand, get_address (msg));
3167         } else {
3168                 s = _select;
3169         }
3170         if (s) {
3171                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3172                 if (track) {
3173                         if (track->monitoring_control()) {
3174                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3175                                 return 0;
3176                         }
3177                 }
3178         }
3179         return sel_fail ("monitor_input", 0, get_address (msg));
3180 }
3181
3182 int
3183 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
3184 {
3185         if (!session) return -1;
3186         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3187         OSCSurface *sur = get_surface(get_address (msg));
3188
3189         if (s) {
3190                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3191                 if (track) {
3192                         if (track->monitoring_control()) {
3193                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, sur->usegroup);
3194                                 return 0;
3195                         }
3196                 }
3197         }
3198
3199         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
3200 }
3201
3202 int
3203 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
3204 {
3205         OSCSurface *sur = get_surface(get_address (msg));
3206         boost::shared_ptr<Stripable> s;
3207         if (sur->expand_enable) {
3208                 s = get_strip (sur->expand, get_address (msg));
3209         } else {
3210                 s = _select;
3211         }
3212         if (s) {
3213                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3214                 if (track) {
3215                         if (track->monitoring_control()) {
3216                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
3217                                 return 0;
3218                         }
3219                 }
3220         }
3221         return sel_fail ("monitor_disk", 0, get_address (msg));
3222 }
3223
3224
3225 int
3226 OSC::strip_phase (int ssid, int yn, lo_message msg)
3227 {
3228         if (!session) return -1;
3229         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3230         OSCSurface *sur = get_surface(get_address (msg));
3231
3232         if (s) {
3233                 if (s->phase_control()) {
3234                         s->phase_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3235                         return 0;
3236                 }
3237         }
3238
3239         return route_send_fail ("polarity", ssid, 0, get_address (msg));
3240 }
3241
3242 int
3243 OSC::sel_phase (uint32_t yn, lo_message msg)
3244 {
3245         OSCSurface *sur = get_surface(get_address (msg));
3246         boost::shared_ptr<Stripable> s;
3247         if (sur->expand_enable) {
3248                 s = get_strip (sur->expand, get_address (msg));
3249         } else {
3250                 s = _select;
3251         }
3252         if (s) {
3253                 if (s->phase_control()) {
3254                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3255                         return 0;
3256                 }
3257         }
3258         return sel_fail ("polarity", 0, get_address (msg));
3259 }
3260
3261 int
3262 OSC::strip_expand (int ssid, int yn, lo_message msg)
3263 {
3264         OSCSurface *sur = get_surface(get_address (msg));
3265         sur->expand_enable = (bool) yn;
3266         sur->expand = ssid;
3267         boost::shared_ptr<Stripable> s;
3268         if (yn) {
3269                 s = get_strip (ssid, get_address (msg));
3270         } else {
3271                 s = ControlProtocol::first_selected_stripable();
3272         }
3273
3274         return _strip_select (s, get_address (msg));
3275 }
3276
3277 int
3278 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
3279 {
3280         if (!session) {
3281                 return -1;
3282         }
3283         OSCSurface *sur = get_surface(addr);
3284         if (sur->sel_obs) {
3285                 delete sur->sel_obs;
3286                 sur->sel_obs = 0;
3287         }
3288         bool feedback_on = sur->feedback[13];
3289         if (s && feedback_on) {
3290                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur);
3291                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
3292                 sur->sel_obs = sel_fb;
3293         } else if (sur->expand_enable) {
3294                 // expand doesn't point to a stripable, turn it off and use select
3295                 sur->expand = 0;
3296                 sur->expand_enable = false;
3297                 if (_select && feedback_on) {
3298                         s = _select;
3299                         OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur);
3300                         s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
3301                         sur->sel_obs = sel_fb;
3302                 }
3303         } else if (feedback_on) {
3304                 route_send_fail ("select", sur->expand, 0 , addr);
3305         }
3306         // need to set monitor for processor changed signal
3307         // detecting processor changes requires cast to route
3308         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
3309         if (r) {
3310                 r->processors_changed.connect  (sur->proc_connection, MISSING_INVALIDATOR, boost::bind (&OSC::processor_changed, this, addr), this);
3311                 processor_changed (addr);
3312         }
3313
3314         if (!feedback_on) {
3315                 return 0;
3316         }
3317         //update buttons on surface
3318         int b_s = sur->bank_size;
3319         if (!b_s) { // bank size 0 means we need to know how many strips there are.
3320                 b_s = sur->nstrips;
3321         }
3322         for (int i = 1;  i <= b_s; i++) {
3323                 string path = "expand";
3324
3325                 if ((i == (int) sur->expand) && sur->expand_enable) {
3326                         lo_message reply = lo_message_new ();
3327                         if (sur->feedback[2]) {
3328                                 ostringstream os;
3329                                 os << "/strip/" << path << "/" << i;
3330                                 path = os.str();
3331                         } else {
3332                                 ostringstream os;
3333                                 os << "/strip/" << path;
3334                                 path = os.str();
3335                                 lo_message_add_int32 (reply, i);
3336                         }
3337                         lo_message_add_float (reply, (float) 1);
3338
3339                         lo_send_message (addr, path.c_str(), reply);
3340                         lo_message_free (reply);
3341                         reply = lo_message_new ();
3342                         lo_message_add_float (reply, 1.0);
3343                         lo_send_message (addr, "/select/expand", reply);
3344                         lo_message_free (reply);
3345
3346                 } else {
3347                         lo_message reply = lo_message_new ();
3348                         lo_message_add_int32 (reply, i);
3349                         lo_message_add_float (reply, 0.0);
3350                         lo_send_message (addr, "/strip/expand", reply);
3351                         lo_message_free (reply);
3352                 }
3353         }
3354         if (!sur->expand_enable) {
3355                 lo_message reply = lo_message_new ();
3356                 lo_message_add_float (reply, 0.0);
3357                 lo_send_message (addr, "/select/expand", reply);
3358                 lo_message_free (reply);
3359         }
3360
3361         return 0;
3362 }
3363
3364 void
3365 OSC::processor_changed (lo_address addr)
3366 {
3367         OSCSurface *sur = get_surface (addr);
3368         sur->proc_connection.disconnect ();
3369         _sel_plugin (sur->plugin_id, addr);
3370         if (sur->sel_obs) {
3371                 sur->sel_obs->renew_sends ();
3372                 sur->sel_obs->eq_restart (-1);
3373         }
3374 }
3375
3376 int
3377 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
3378 {
3379         //ignore button release
3380         if (!yn) return 0;
3381
3382         if (!session) {
3383                 return -1;
3384         }
3385         OSCSurface *sur = get_surface(get_address (msg));
3386         sur->expand_enable = false;
3387         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3388         if (s) {
3389                 SetStripableSelection (s);
3390         } else {
3391                 if ((int) (sur->feedback.to_ulong())) {
3392                         route_send_fail ("select", ssid, 0, get_address (msg));
3393                 }
3394         }
3395
3396         return 0;
3397 }
3398
3399 int
3400 OSC::sel_expand (uint32_t state, lo_message msg)
3401 {
3402         OSCSurface *sur = get_surface(get_address (msg));
3403         boost::shared_ptr<Stripable> s;
3404         sur->expand_enable = (bool) state;
3405         if (state && sur->expand) {
3406                 s = get_strip (sur->expand, get_address (msg));
3407         } else {
3408                 s = ControlProtocol::first_selected_stripable();
3409         }
3410
3411         return _strip_select (s, get_address (msg));
3412 }
3413
3414 int
3415 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
3416 {
3417         if (!session) return -1;
3418         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3419         OSCSurface *sur = get_surface(get_address (msg));
3420
3421         if (s) {
3422                 if (s->gain_control()) {
3423                         fake_touch (s->gain_control());
3424                         s->gain_control()->set_value (level, sur->usegroup);
3425                 } else {
3426                         return 1;
3427                 }
3428         } else {
3429                 return 1;
3430         }
3431
3432         return 0;
3433 }
3434
3435 int
3436 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
3437 {
3438         if (!session) {
3439                 route_send_fail ("gain", ssid, -193, get_address (msg));
3440                 return -1;
3441         }
3442         int ret;
3443         if (dB < -192) {
3444                 ret = route_set_gain_abs (ssid, 0.0, msg);
3445         } else {
3446                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
3447         }
3448         if (ret != 0) {
3449                 return route_send_fail ("gain", ssid, -193, get_address (msg));
3450         }
3451         return 0;
3452 }
3453
3454 int
3455 OSC::sel_gain (float val, lo_message msg)
3456 {
3457         OSCSurface *sur = get_surface(get_address (msg));
3458         boost::shared_ptr<Stripable> s;
3459         if (sur->expand_enable) {
3460                 s = get_strip (sur->expand, get_address (msg));
3461         } else {
3462                 s = _select;
3463         }
3464         if (s) {
3465                 float abs;
3466                 if (s->gain_control()) {
3467                         if (val < -192) {
3468                                 abs = 0;
3469                         } else {
3470                                 abs = dB_to_coefficient (val);
3471                                 float top = s->gain_control()->upper();
3472                                 if (abs > top) {
3473                                         abs = top;
3474                                 }
3475                         }
3476                         fake_touch (s->gain_control());
3477                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3478                         return 0;
3479                 }
3480         }
3481         return sel_fail ("gain", -193, get_address (msg));
3482 }
3483
3484 int
3485 OSC::sel_dB_delta (float delta, lo_message msg)
3486 {
3487         OSCSurface *sur = get_surface(get_address (msg));
3488         boost::shared_ptr<Stripable> s;
3489         if (sur->expand_enable) {
3490                 s = get_strip (sur->expand, get_address (msg));
3491         } else {
3492                 s = _select;
3493         }
3494         if (s) {
3495                 if (s->gain_control()) {
3496                         float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
3497                         float abs;
3498                         if (dB < -192) {
3499                                 abs = 0;
3500                         } else {
3501                                 abs = dB_to_coefficient (dB);
3502                                 float top = s->gain_control()->upper();
3503                                 if (abs > top) {
3504                                         abs = top;
3505                                 }
3506                         }
3507                         fake_touch (s->gain_control());
3508                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3509                         return 0;
3510                 }
3511         }
3512         return sel_fail ("gain", -193, get_address (msg));
3513 }
3514
3515 int
3516 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
3517 {
3518         if (!session) {
3519                 return -1;
3520         }
3521         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3522         OSCSurface *sur = get_surface(get_address (msg));
3523
3524         if (s) {
3525                 if (s->gain_control()) {
3526                         fake_touch (s->gain_control());
3527                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (pos), sur->usegroup);
3528                 } else {
3529                         return route_send_fail ("fader", ssid, 0, get_address (msg));
3530                 }
3531         } else {
3532                 return route_send_fail ("fader", ssid, 0, get_address (msg));
3533         }
3534         return 0;
3535 }
3536
3537 int
3538 OSC::strip_db_delta (int ssid, float delta, lo_message msg)
3539 {
3540         if (!session) return -1;
3541         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3542         OSCSurface *sur = get_surface(get_address (msg));
3543         if (s) {
3544                 float db = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
3545                 float abs;
3546                 if (db < -192) {
3547                         abs = 0;
3548                 } else {
3549                         abs = dB_to_coefficient (db);
3550                         float top = s->gain_control()->upper();
3551                         if (abs > top) {
3552                                 abs = top;
3553                         }
3554                 }
3555                 s->gain_control()->set_value (abs, sur->usegroup);
3556                 return 0;
3557         }
3558         return -1;
3559 }
3560
3561 int
3562 OSC::sel_fader (float val, lo_message msg)
3563 {
3564         OSCSurface *sur = get_surface(get_address (msg));
3565         boost::shared_ptr<Stripable> s;
3566         if (sur->expand_enable) {
3567                 s = get_strip (sur->expand, get_address (msg));
3568         } else {
3569                 s = _select;
3570         }
3571         if (s) {
3572                 if (s->gain_control()) {
3573                         fake_touch (s->gain_control());
3574                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3575                         return 0;
3576                 }
3577         }
3578         return sel_fail ("fader", 0, get_address (msg));
3579 }
3580
3581 int
3582 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
3583 {
3584         if (!session) return -1;
3585         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3586         OSCSurface *sur = get_surface(get_address (msg));
3587
3588         if (s) {
3589                 if (s->trim_control()) {
3590                         s->trim_control()->set_value (level, sur->usegroup);
3591                         return 0;
3592                 }
3593
3594         }
3595
3596         return -1;
3597 }
3598
3599 int
3600 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
3601 {
3602         int ret;
3603         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
3604         if (ret != 0) {
3605                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
3606         }
3607
3608 return 0;
3609 }
3610
3611 int
3612 OSC::sel_trim (float val, lo_message msg)
3613 {
3614         OSCSurface *sur = get_surface(get_address (msg));
3615         boost::shared_ptr<Stripable> s;
3616         if (sur->expand_enable) {
3617                 s = get_strip (sur->expand, get_address (msg));
3618         } else {
3619                 s = _select;
3620         }
3621         if (s) {
3622                 if (s->trim_control()) {
3623                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
3624                         return 0;
3625                 }
3626         }
3627         return sel_fail ("trimdB", 0, get_address (msg));
3628 }
3629
3630 int
3631 OSC::sel_pan_position (float val, lo_message msg)
3632 {
3633         OSCSurface *sur = get_surface(get_address (msg));
3634         boost::shared_ptr<Stripable> s;
3635         if (sur->expand_enable) {
3636                 s = get_strip (sur->expand, get_address (msg));
3637         } else {
3638                 s = _select;
3639         }
3640         if (s) {
3641                 if(s->pan_azimuth_control()) {
3642                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3643                         return 0;
3644                 }
3645         }
3646         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
3647 }
3648
3649 int
3650 OSC::sel_pan_width (float val, lo_message msg)
3651 {
3652         OSCSurface *sur = get_surface(get_address (msg));
3653         boost::shared_ptr<Stripable> s;
3654         if (sur->expand_enable) {
3655                 s = get_strip (sur->expand, get_address (msg));
3656         } else {
3657                 s = _select;
3658         }
3659         if (s) {
3660                 if (s->pan_width_control()) {
3661                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3662                         return 0;
3663                 }
3664         }
3665         return sel_fail ("pan_stereo_width", 1, get_address (msg));
3666 }
3667
3668 int
3669 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
3670 {
3671         if (!session) return -1;
3672         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3673         OSCSurface *sur = get_surface(get_address (msg));
3674
3675         if (s) {
3676                 if(s->pan_azimuth_control()) {
3677                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), sur->usegroup);
3678                         return 0;
3679                 }
3680         }
3681
3682         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
3683 }
3684
3685 int
3686 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
3687 {
3688         if (!session) return -1;
3689         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3690         OSCSurface *sur = get_surface(get_address (msg));
3691
3692         if (s) {
3693                 if (s->pan_width_control()) {
3694                         s->pan_width_control()->set_value (pos, sur->usegroup);
3695                         return 0;
3696                 }
3697         }
3698
3699         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
3700 }
3701
3702 int
3703 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
3704 {
3705         if (!session) {
3706                 return -1;
3707         }
3708         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3709         OSCSurface *sur = get_surface(get_address (msg));
3710         float abs;
3711         if (s) {
3712                 if (id > 0) {
3713                         --id;
3714                 }
3715 #ifdef MIXBUS
3716                 abs = val;
3717 #else
3718                 if (val < -192) {
3719                         abs = 0;
3720                 } else {
3721                         abs = dB_to_coefficient (val);
3722                 }
3723 #endif
3724                 if (s->send_level_controllable (id)) {
3725                         s->send_level_controllable (id)->set_value (abs, sur->usegroup);
3726                         return 0;
3727                 }
3728         }
3729         return 0;
3730 }
3731
3732 int
3733 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
3734 {
3735         if (!session) {
3736                 return -1;
3737         }
3738         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3739         OSCSurface *sur = get_surface(get_address (msg));
3740         float abs;
3741         if (s) {
3742
3743                 if (id > 0) {
3744                         --id;
3745                 }
3746
3747                 if (s->send_level_controllable (id)) {
3748                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3749                         s->send_level_controllable (id)->set_value (abs, sur->usegroup);
3750                         return 0;
3751                 }
3752         }
3753         return 0;
3754 }
3755
3756 int
3757 OSC::sel_sendgain (int id, float val, lo_message msg)
3758 {
3759         OSCSurface *sur = get_surface(get_address (msg));
3760         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3761                 return sel_send_fail ("send_gain", id, -193, get_address (msg));
3762         }
3763         boost::shared_ptr<Stripable> s;
3764         if (sur->expand_enable) {
3765                 s = get_strip (sur->expand, get_address (msg));
3766         } else {
3767                 s = _select;
3768         }
3769         float abs;
3770         int send_id = 0;
3771         if (s) {
3772                 if (id > 0) {
3773                         send_id = id - 1;
3774                 }
3775 #ifdef MIXBUS
3776                 abs = val;
3777 #else
3778                 if (val < -192) {
3779                         abs = 0;
3780                 } else {
3781                         abs = dB_to_coefficient (val);
3782                 }
3783 #endif
3784                 if (sur->send_page_size) {
3785                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3786                 }
3787                 if (s->send_level_controllable (send_id)) {
3788                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
3789                         return 0;
3790                 }
3791         }
3792         return sel_send_fail ("send_gain", id, -193, get_address (msg));
3793 }
3794
3795 int
3796 OSC::sel_sendfader (int id, float val, lo_message msg)
3797 {
3798         OSCSurface *sur = get_surface(get_address (msg));
3799         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3800                 return sel_send_fail ("send_fader", id, 0, get_address (msg));
3801         }
3802         boost::shared_ptr<Stripable> s;
3803         if (sur->expand_enable) {
3804                 s = get_strip (sur->expand, get_address (msg));
3805         } else {
3806                 s = _select;
3807         }
3808         float abs;
3809         int send_id = 0;
3810         if (s) {
3811
3812                 if (id > 0) {
3813                         send_id = id - 1;
3814                 }
3815                 if (sur->send_page_size) {
3816                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3817                 }
3818
3819                 if (s->send_level_controllable (send_id)) {
3820                         abs = s->send_level_controllable(send_id)->interface_to_internal (val);
3821                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
3822                         return 0;
3823                 }
3824         }
3825         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3826 }
3827
3828 int
3829 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3830 {
3831         if (!session) {
3832                 return -1;
3833         }
3834         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3835         OSCSurface *sur = get_surface(get_address (msg));
3836
3837         if (s) {
3838
3839                 /* revert to zero-based counting */
3840
3841                 if (sid > 0) {
3842                         --sid;
3843                 }
3844
3845                 if (s->send_enable_controllable (sid)) {
3846                         s->send_enable_controllable (sid)->set_value (val, sur->usegroup);
3847                         return 0;
3848                 }
3849
3850                 if (s->send_level_controllable (sid)) {
3851                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3852                         if (!r) {
3853                                 return 0;
3854                         }
3855                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3856                         if (snd) {
3857                                 if (val) {
3858                                         snd->activate();
3859                                 } else {
3860                                         snd->deactivate();
3861                                 }
3862                         }
3863                         return 0;
3864                 }
3865
3866         }
3867
3868         return -1;
3869 }
3870
3871 int
3872 OSC::sel_sendenable (int id, float val, lo_message msg)
3873 {
3874         OSCSurface *sur = get_surface(get_address (msg));
3875         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3876                 return sel_send_fail ("send_enable", id, 0, get_address (msg));
3877         }
3878         boost::shared_ptr<Stripable> s;
3879         if (sur->expand_enable) {
3880                 s = get_strip (sur->expand, get_address (msg));
3881         } else {
3882                 s = _select;
3883         }
3884         int send_id = 0;
3885         if (s) {
3886                 if (id > 0) {
3887                         send_id = id - 1;
3888                 }
3889                 if (sur->send_page_size) {
3890                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3891                 }
3892                 if (s->send_enable_controllable (send_id)) {
3893                         s->send_enable_controllable (send_id)->set_value (val, PBD::Controllable::NoGroup);
3894                         return 0;
3895                 }
3896                 if (s->send_level_controllable (send_id)) {
3897                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3898                         if (!r) {
3899                                 // should never get here
3900                                 return sel_send_fail ("send_enable", id, 0, get_address (msg));
3901                         }
3902                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(send_id));
3903                         if (snd) {
3904                                 if (val) {
3905                                         snd->activate();
3906                                 } else {
3907                                         snd->deactivate();
3908                                 }
3909                         }
3910                         return 0;
3911                 }
3912         }
3913         return sel_send_fail ("send_enable", id, 0, get_address (msg));
3914 }
3915
3916 int
3917 OSC::sel_master_send_enable (int state, lo_message msg)
3918 {
3919         OSCSurface *sur = get_surface(get_address (msg));
3920         boost::shared_ptr<Stripable> s;
3921         if (sur->expand_enable) {
3922                 s = get_strip (sur->expand, get_address (msg));
3923         } else {
3924                 s = _select;
3925         }
3926         if (s) {
3927                 if (s->master_send_enable_controllable ()) {
3928                         s->master_send_enable_controllable()->set_value (state, PBD::Controllable::NoGroup);
3929                         return 0;
3930                 }
3931         }
3932         return cue_float_message ("/select/master_send_enable", 0, get_address(msg));
3933 }
3934
3935 int
3936 OSC::select_plugin_parameter (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg) {
3937         OSCSurface *sur = get_surface(get_address (msg));
3938         int paid;
3939         int piid = sur->plugin_id;
3940         float value = 0;
3941         if (argc > 1) {
3942                 // no inline args
3943                 if (argc == 2) {
3944                         // change parameter in already selected plugin
3945                         if (types[0]  == 'f') {
3946                                 paid = (int) argv[0]->f;
3947                         } else {
3948                                 paid = argv[0]->i;
3949                         }
3950                         value = argv[1]->f;
3951                 } else if (argc == 3) {
3952                         if (types[0] == 'f') {
3953                                 piid = (int) argv[0]->f;
3954                         } else {
3955                                 piid = argv[0]->i;
3956                         }
3957                         _sel_plugin (piid, get_address (msg));
3958                         if (types[1] == 'f') {
3959                                 paid = (int) argv[1]->f;
3960                         } else {
3961                                 paid = argv[1]->i;
3962                         }
3963                         value = argv[2]->f;
3964                 } else if (argc > 3) {
3965                         PBD::warning << "OSC: Too many parameters: " << argc << endmsg;
3966                         return -1;
3967                 }
3968         } else if (argc) {
3969                 const char * par = strstr (&path[25], "/");
3970                 if (par) {
3971                         piid = atoi (&path[25]);
3972                         _sel_plugin (piid, msg);
3973                         paid = atoi (&par[1]);
3974                         value = argv[0]->f;
3975                         // we have plugin id too
3976                 } else {
3977                         // just parameter
3978                         paid = atoi (&path[25]);
3979                         value = argv[0]->f;
3980                 }
3981         } else {
3982                 PBD::warning << "OSC: Must have parameters." << endmsg;
3983                 return -1;
3984         }
3985         if (piid != sur->plugin_id) {
3986                 // if the user is sending to a non-existant plugin, don't adjust one we do have
3987                 PBD::warning << "OSC: plugin: " << piid << " out of range" << endmsg;
3988                 return -1;
3989         }
3990         if (sur->plug_page_size && (paid > (int)sur->plug_page_size)) {
3991                 return sel_send_fail ("plugin/parameter", paid, 0, get_address (msg));
3992         }
3993         boost::shared_ptr<Stripable> s;
3994         if (sur->expand_enable) {
3995                 s = get_strip (sur->expand, get_address (msg));
3996         } else {
3997                 s = _select;
3998         }
3999         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
4000         if (!r) {
4001                 return 1;
4002         }
4003
4004         boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
4005         boost::shared_ptr<PluginInsert> pi;
4006         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
4007                 return 1;
4008         }
4009         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4010         // paid is paged parameter convert to absolute
4011         int parid = paid + (int)(sur->plug_page_size * (sur->plug_page - 1));
4012         if (parid > (int) sur->plug_params.size ()) {
4013                 if (sur->feedback[13]) {
4014                         sel_send_fail ("plugin/parameter", paid, 0, get_address (msg));
4015                 }
4016                 return 0;
4017         }
4018
4019         bool ok = false;
4020         uint32_t controlid = pip->nth_parameter(sur->plug_params[parid - 1], ok);
4021         if (!ok) {
4022                 return 1;
4023         }
4024         ParameterDescriptor pd;
4025         pip->get_parameter_descriptor(controlid, pd);
4026         if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
4027                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
4028                 if (c) {
4029                         if (pd.integer_step && pd.upper == 1) {
4030                                 if (c->get_value () && value < 1.0) {
4031                                         c->set_value (0, PBD::Controllable::NoGroup);
4032                                 } else if (!c->get_value () && value) {
4033                                         c->set_value (1, PBD::Controllable::NoGroup);
4034                                 }
4035                         } else {
4036                                 c->set_value (c->interface_to_internal (value), PBD::Controllable::NoGroup);
4037                         }
4038                         return 0;
4039                 }
4040         }
4041         return 1;
4042 }
4043
4044 int
4045 OSC::route_plugin_list (int ssid, lo_message msg) {
4046         if (!session) {
4047                 return -1;
4048         }
4049
4050         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4051
4052         if (!r) {
4053                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4054                 return -1;
4055         }
4056         int piid = 0;
4057
4058         lo_message reply = lo_message_new ();
4059         lo_message_add_int32 (reply, ssid);
4060
4061
4062         for (;;) {
4063                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
4064                 if ( !redi ) {
4065                         break;
4066                 }
4067
4068                 boost::shared_ptr<PluginInsert> pi;
4069
4070                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4071                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4072                         continue;
4073                 }
4074                 lo_message_add_int32 (reply, piid + 1);
4075
4076                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4077                 lo_message_add_string (reply, pip->name());
4078                 lo_message_add_int32(reply, redi->enabled() ? 1 : 0);
4079
4080                 piid++;
4081         }
4082
4083         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
4084         lo_message_free (reply);
4085         return 0;
4086 }
4087
4088 int
4089 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
4090         if (!session) {
4091                 return -1;
4092         }
4093
4094         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4095
4096         if (!r) {
4097                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4098                 return -1;
4099         }
4100
4101         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4102
4103         if (!redi) {
4104                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4105                 return -1;
4106         }
4107
4108         boost::shared_ptr<PluginInsert> pi;
4109
4110         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4111                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4112                 return -1;
4113         }
4114
4115         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4116         bool ok = false;
4117
4118         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
4119
4120                 uint32_t controlid = pip->nth_parameter(ppi, ok);
4121                 if (!ok) {
4122                         continue;
4123                 }
4124                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
4125
4126                 lo_message reply = lo_message_new();
4127                 lo_message_add_int32 (reply, ssid);
4128                 lo_message_add_int32 (reply, piid);
4129
4130                 lo_message_add_int32 (reply, ppi + 1);
4131                 ParameterDescriptor pd;
4132                 pi->plugin()->get_parameter_descriptor(controlid, pd);
4133                 lo_message_add_string (reply, pd.label.c_str());
4134
4135                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
4136                 int flags = 0;
4137                 flags |= pd.enumeration ? 1 : 0;
4138                 flags |= pd.integer_step ? 2 : 0;
4139                 flags |= pd.logarithmic ? 4 : 0;
4140                 flags |= pd.sr_dependent ? 32 : 0;
4141                 flags |= pd.toggled ? 64 : 0;
4142                 flags |= pip->parameter_is_input(controlid) ? 0x80 : 0;
4143
4144                 std::string param_desc = pi->plugin()->describe_parameter(Evoral::Parameter(PluginAutomation, 0, controlid));
4145                 flags |= (param_desc == X_("hidden")) ? 0x100 : 0;
4146                 lo_message_add_int32 (reply, flags);
4147
4148                 switch(pd.datatype) {
4149                         case ARDOUR::Variant::BEATS:
4150                                 lo_message_add_string(reply, _("BEATS"));
4151                                 break;
4152                         case ARDOUR::Variant::BOOL:
4153                                 lo_message_add_string(reply, _("BOOL"));
4154                                 break;
4155                         case ARDOUR::Variant::DOUBLE:
4156                                 lo_message_add_string(reply, _("DOUBLE"));
4157                                 break;
4158                         case ARDOUR::Variant::FLOAT:
4159                                 lo_message_add_string(reply, _("FLOAT"));
4160                                 break;
4161                         case ARDOUR::Variant::INT:
4162                                 lo_message_add_string(reply, _("INT"));
4163                                 break;
4164                         case ARDOUR::Variant::LONG:
4165                                 lo_message_add_string(reply, _("LONG"));
4166                                 break;
4167                         case ARDOUR::Variant::NOTHING:
4168                                 lo_message_add_string(reply, _("NOTHING"));
4169                                 break;
4170                         case ARDOUR::Variant::PATH:
4171                                 lo_message_add_string(reply, _("PATH"));
4172                                 break;
4173                         case ARDOUR::Variant::STRING:
4174                                 lo_message_add_string(reply, _("STRING"));
4175                                 break;
4176                         case ARDOUR::Variant::URI:
4177                                 lo_message_add_string(reply, _("URI"));
4178                                 break;
4179                         default:
4180                                 lo_message_add_string(reply, _("UNKNOWN"));
4181                                 break;
4182                 }
4183                 lo_message_add_float (reply, pd.lower);
4184                 lo_message_add_float (reply, pd.upper);
4185                 lo_message_add_string (reply, pd.print_fmt.c_str());
4186                 if ( pd.scale_points ) {
4187                         lo_message_add_int32 (reply, pd.scale_points->size());
4188                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
4189                                 lo_message_add_float (reply, i->second);
4190                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
4191                         }
4192                 }
4193                 else {
4194                         lo_message_add_int32 (reply, 0);
4195                 }
4196                 if ( c ) {
4197                         lo_message_add_double (reply, c->get_value());
4198                 }
4199                 else {
4200                         lo_message_add_double (reply, 0);
4201                 }
4202
4203                 lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
4204                 lo_message_free (reply);
4205         }
4206
4207         lo_message reply = lo_message_new ();
4208         lo_message_add_int32 (reply, ssid);
4209         lo_message_add_int32 (reply, piid);
4210         lo_send_message (get_address (msg), "/strip/plugin/descriptor_end", reply);
4211         lo_message_free (reply);
4212
4213         return 0;
4214 }
4215
4216 int
4217 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
4218         if (!session) {
4219                 return -1;
4220         }
4221
4222         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4223
4224         if (!r) {
4225                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4226                 return -1;
4227         }
4228
4229         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4230
4231         if (!redi) {
4232                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4233                 return -1;
4234         }
4235
4236         boost::shared_ptr<PluginInsert> pi;
4237
4238         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4239                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4240                 return -1;
4241         }
4242
4243         pi->reset_parameters_to_default ();
4244
4245         return 0;
4246 }
4247
4248 int
4249 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
4250 {
4251         if (!session)
4252                 return -1;
4253         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4254
4255         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4256
4257         if (!r) {
4258                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4259                 return -1;
4260         }
4261
4262         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4263
4264         if (!redi) {
4265                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4266                 return -1;
4267         }
4268
4269         boost::shared_ptr<PluginInsert> pi;
4270
4271         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4272                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4273                 return -1;
4274         }
4275
4276         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4277         bool ok=false;
4278
4279         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4280
4281         if (!ok) {
4282                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
4283                 return -1;
4284         }
4285
4286         if (!pip->parameter_is_input(controlid)) {
4287                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
4288                 return -1;
4289         }
4290
4291         ParameterDescriptor pd;
4292         pi->plugin()->get_parameter_descriptor (controlid,pd);
4293
4294         if (val >= pd.lower && val <= pd.upper) {
4295
4296                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4297                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
4298                 c->set_value (val, PBD::Controllable::NoGroup);
4299         } else {
4300                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
4301                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
4302         }
4303
4304         return 0;
4305 }
4306
4307 //prints to cerr only
4308 int
4309 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
4310 {
4311         if (!session) {
4312                 return -1;
4313         }
4314         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4315
4316         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4317
4318         if (!r) {
4319                 return -1;
4320         }
4321
4322         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4323
4324         if (!redi) {
4325                 return -1;
4326         }
4327
4328         boost::shared_ptr<PluginInsert> pi;
4329
4330         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4331                 return -1;
4332         }
4333
4334         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4335         bool ok=false;
4336
4337         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4338
4339         if (!ok) {
4340                 return -1;
4341         }
4342
4343         ParameterDescriptor pd;
4344
4345         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
4346                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4347
4348                 cerr << "parameter:     " << pd.label  << "\n";
4349                 if (c) {
4350                         cerr << "current value: " << c->get_value () << "\n";
4351                 } else {
4352                         cerr << "current value not available, control does not exist\n";
4353                 }
4354                 cerr << "lower value:   " << pd.lower << "\n";
4355                 cerr << "upper value:   " << pd.upper << "\n";
4356         }
4357
4358         return 0;
4359 }
4360
4361 int
4362 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
4363 {
4364         if (!session)
4365                 return -1;
4366         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4367
4368         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4369
4370         if (!r) {
4371                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4372                 return -1;
4373         }
4374
4375         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4376
4377         if (!redi) {
4378                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4379                 return -1;
4380         }
4381
4382         boost::shared_ptr<PluginInsert> pi;
4383
4384         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4385                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4386                 return -1;
4387         }
4388
4389         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4390         pi->activate();
4391
4392         return 0;
4393 }
4394
4395 int
4396 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
4397 {
4398         if (!session)
4399                 return -1;
4400         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4401
4402         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4403
4404         if (!r) {
4405                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4406                 return -1;
4407         }
4408
4409         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4410
4411         if (!redi) {
4412                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4413                 return -1;
4414         }
4415
4416         boost::shared_ptr<PluginInsert> pi;
4417
4418         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4419                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4420                 return -1;
4421         }
4422
4423         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4424         pi->deactivate();
4425
4426         return 0;
4427 }
4428
4429 // select
4430
4431 int
4432 OSC::sel_pan_elevation (float val, lo_message msg)
4433 {
4434         OSCSurface *sur = get_surface(get_address (msg));
4435         boost::shared_ptr<Stripable> s;
4436         if (sur->expand_enable) {
4437                 s = get_strip (sur->expand, get_address (msg));
4438         } else {
4439                 s = _select;
4440         }
4441         if (s) {
4442                 if (s->pan_elevation_control()) {
4443                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4444                         return 0;
4445                 }
4446         }
4447         return sel_fail ("pan_elevation_position", 0, get_address (msg));
4448 }
4449
4450 int
4451 OSC::sel_pan_frontback (float val, lo_message msg)
4452 {
4453         OSCSurface *sur = get_surface(get_address (msg));
4454         boost::shared_ptr<Stripable> s;
4455         if (sur->expand_enable) {
4456                 s = get_strip (sur->expand, get_address (msg));
4457         } else {
4458                 s = _select;
4459         }
4460         if (s) {
4461                 if (s->pan_frontback_control()) {
4462                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4463                         return 0;
4464                 }
4465         }
4466         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
4467 }
4468
4469 int
4470 OSC::sel_pan_lfe (float val, lo_message msg)
4471 {
4472         OSCSurface *sur = get_surface(get_address (msg));
4473         boost::shared_ptr<Stripable> s;
4474         if (sur->expand_enable) {
4475                 s = get_strip (sur->expand, get_address (msg));
4476         } else {
4477                 s = _select;
4478         }
4479         if (s) {
4480                 if (s->pan_lfe_control()) {
4481                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4482                         return 0;
4483                 }
4484         }
4485         return sel_fail ("pan_lfe_control", 0, get_address (msg));
4486 }
4487
4488 // compressor control
4489 int
4490 OSC::sel_comp_enable (float val, lo_message msg)
4491 {
4492         OSCSurface *sur = get_surface(get_address (msg));
4493         boost::shared_ptr<Stripable> s;
4494         if (sur->expand_enable) {
4495                 s = get_strip (sur->expand, get_address (msg));
4496         } else {
4497                 s = _select;
4498         }
4499         if (s) {
4500                 if (s->comp_enable_controllable()) {
4501                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4502                         return 0;
4503                 }
4504         }
4505         return sel_fail ("comp_enable", 0, get_address (msg));
4506 }
4507
4508 int
4509 OSC::sel_comp_threshold (float val, lo_message msg)
4510 {
4511         OSCSurface *sur = get_surface(get_address (msg));
4512         boost::shared_ptr<Stripable> s;
4513         if (sur->expand_enable) {
4514                 s = get_strip (sur->expand, get_address (msg));
4515         } else {
4516                 s = _select;
4517         }
4518         if (s) {
4519                 if (s->comp_threshold_controllable()) {
4520                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4521                         return 0;
4522                 }
4523         }
4524         return sel_fail ("comp_threshold", 0, get_address (msg));
4525 }
4526
4527 int
4528 OSC::sel_comp_speed (float val, lo_message msg)
4529 {
4530         OSCSurface *sur = get_surface(get_address (msg));
4531         boost::shared_ptr<Stripable> s;
4532         if (sur->expand_enable) {
4533                 s = get_strip (sur->expand, get_address (msg));
4534         } else {
4535                 s = _select;
4536         }
4537         if (s) {
4538                 if (s->comp_speed_controllable()) {
4539                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4540                         return 0;
4541                 }
4542         }
4543         return sel_fail ("comp_speed", 0, get_address (msg));
4544 }
4545
4546 int
4547 OSC::sel_comp_mode (float val, lo_message msg)
4548 {
4549         OSCSurface *sur = get_surface(get_address (msg));
4550         boost::shared_ptr<Stripable> s;
4551         if (sur->expand_enable) {
4552                 s = get_strip (sur->expand, get_address (msg));
4553         } else {
4554                 s = _select;
4555         }
4556         if (s) {
4557                 if (s->comp_mode_controllable()) {
4558                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4559                         return 0;
4560                 }
4561         }
4562         return sel_fail ("comp_mode", 0, get_address (msg));
4563 }
4564
4565 int
4566 OSC::sel_comp_makeup (float val, lo_message msg)
4567 {
4568         OSCSurface *sur = get_surface(get_address (msg));
4569         boost::shared_ptr<Stripable> s;
4570         if (sur->expand_enable) {
4571                 s = get_strip (sur->expand, get_address (msg));
4572         } else {
4573                 s = _select;
4574         }
4575         if (s) {
4576                 if (s->comp_makeup_controllable()) {
4577                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4578                         return 0;
4579                 }
4580         }
4581         return sel_fail ("comp_makeup", 0, get_address (msg));
4582 }
4583
4584 // EQ control
4585
4586 int
4587 OSC::sel_eq_enable (float val, lo_message msg)
4588 {
4589         OSCSurface *sur = get_surface(get_address (msg));
4590         boost::shared_ptr<Stripable> s;
4591         if (sur->expand_enable) {
4592                 s = get_strip (sur->expand, get_address (msg));
4593         } else {
4594                 s = _select;
4595         }
4596         if (s) {
4597                 if (s->eq_enable_controllable()) {
4598                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4599                         return 0;
4600                 }
4601         }
4602         return sel_fail ("eq_enable", 0, get_address (msg));
4603 }
4604
4605 int
4606 OSC::sel_eq_hpf_freq (float val, lo_message msg)
4607 {
4608         OSCSurface *sur = get_surface(get_address (msg));
4609         boost::shared_ptr<Stripable> s;
4610         if (sur->expand_enable) {
4611                 s = get_strip (sur->expand, get_address (msg));
4612         } else {
4613                 s = _select;
4614         }
4615         if (s) {
4616                 if (s->filter_freq_controllable(true)) {
4617                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4618                         return 0;
4619                 }
4620         }
4621         return sel_fail ("eq_hpf/freq", 0, get_address (msg));
4622 }
4623
4624 int
4625 OSC::sel_eq_lpf_freq (float val, lo_message msg)
4626 {
4627         OSCSurface *sur = get_surface(get_address (msg));
4628         boost::shared_ptr<Stripable> s;
4629         if (sur->expand_enable) {
4630                 s = get_strip (sur->expand, get_address (msg));
4631         } else {
4632                 s = _select;
4633         }
4634         if (s) {
4635                 if (s->filter_freq_controllable(false)) {
4636                         s->filter_freq_controllable(false)->set_value (s->filter_freq_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4637                         return 0;
4638                 }
4639         }
4640         return sel_fail ("eq_lpf/freq", 0, get_address (msg));
4641 }
4642
4643 int
4644 OSC::sel_eq_hpf_enable (float val, lo_message msg)
4645 {
4646         OSCSurface *sur = get_surface(get_address (msg));
4647         boost::shared_ptr<Stripable> s;
4648         if (sur->expand_enable) {
4649                 s = get_strip (sur->expand, get_address (msg));
4650         } else {
4651                 s = _select;
4652         }
4653         if (s) {
4654                 if (s->filter_enable_controllable(true)) {
4655                         s->filter_enable_controllable(true)->set_value (s->filter_enable_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4656                         return 0;
4657                 }
4658         }
4659         return sel_fail ("eq_hpf/enable", 0, get_address (msg));
4660 }
4661
4662 int
4663 OSC::sel_eq_lpf_enable (float val, lo_message msg)
4664 {
4665         OSCSurface *sur = get_surface(get_address (msg));
4666         boost::shared_ptr<Stripable> s;
4667         if (sur->expand_enable) {
4668                 s = get_strip (sur->expand, get_address (msg));
4669         } else {
4670                 s = _select;
4671         }
4672         if (s) {
4673                 if (s->filter_enable_controllable(false)) {
4674                         s->filter_enable_controllable(false)->set_value (s->filter_enable_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4675                         return 0;
4676                 }
4677         }
4678         return sel_fail ("eq_lpf/enable", 0, get_address (msg));
4679 }
4680
4681 int
4682 OSC::sel_eq_hpf_slope (float val, lo_message msg)
4683 {
4684         OSCSurface *sur = get_surface(get_address (msg));
4685         boost::shared_ptr<Stripable> s;
4686         if (sur->expand_enable) {
4687                 s = get_strip (sur->expand, get_address (msg));
4688         } else {
4689                 s = _select;
4690         }
4691         if (s) {
4692                 if (s->filter_slope_controllable(true)) {
4693                         s->filter_slope_controllable(true)->set_value (s->filter_slope_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4694                         return 0;
4695                 }
4696         }
4697         return sel_fail ("eq_hpf/slope", 0, get_address (msg));
4698 }
4699
4700 int
4701 OSC::sel_eq_lpf_slope (float val, lo_message msg)
4702 {
4703         OSCSurface *sur = get_surface(get_address (msg));
4704         boost::shared_ptr<Stripable> s;
4705         if (sur->expand_enable) {
4706                 s = get_strip (sur->expand, get_address (msg));
4707         } else {
4708                 s = _select;
4709         }
4710         if (s) {
4711                 if (s->filter_slope_controllable(false)) {
4712                         s->filter_slope_controllable(false)->set_value (s->filter_slope_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4713                         return 0;
4714                 }
4715         }
4716         return sel_fail ("eq_lpf/slope", 0, get_address (msg));
4717 }
4718
4719 int
4720 OSC::sel_eq_gain (int id, float val, lo_message msg)
4721 {
4722         OSCSurface *sur = get_surface(get_address (msg));
4723         boost::shared_ptr<Stripable> s;
4724         if (sur->expand_enable) {
4725                 s = get_strip (sur->expand, get_address (msg));
4726         } else {
4727                 s = _select;
4728         }
4729         if (s) {
4730                 if (id > 0) {
4731                         --id;
4732                 }
4733                 if (s->eq_gain_controllable (id)) {
4734                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4735                         return 0;
4736                 }
4737         }
4738         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
4739 }
4740
4741 int
4742 OSC::sel_eq_freq (int id, float val, lo_message msg)
4743 {
4744         OSCSurface *sur = get_surface(get_address (msg));
4745         boost::shared_ptr<Stripable> s;
4746         if (sur->expand_enable) {
4747                 s = get_strip (sur->expand, get_address (msg));
4748         } else {
4749                 s = _select;
4750         }
4751         if (s) {
4752                 if (id > 0) {
4753                         --id;
4754                 }
4755                 if (s->eq_freq_controllable (id)) {
4756                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4757                         return 0;
4758                 }
4759         }
4760         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
4761 }
4762
4763 int
4764 OSC::sel_eq_q (int id, float val, lo_message msg)
4765 {
4766         OSCSurface *sur = get_surface(get_address (msg));
4767         boost::shared_ptr<Stripable> s;
4768         if (sur->expand_enable) {
4769                 s = get_strip (sur->expand, get_address (msg));
4770         } else {
4771                 s = _select;
4772         }
4773         if (s) {
4774                 if (id > 0) {
4775                         --id;
4776                 }
4777                 if (s->eq_q_controllable (id)) {
4778                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4779                         return 0;
4780                 }
4781         }
4782         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
4783 }
4784
4785 int
4786 OSC::sel_eq_shape (int id, float val, lo_message msg)
4787 {
4788         OSCSurface *sur = get_surface(get_address (msg));
4789         boost::shared_ptr<Stripable> s;
4790         if (sur->expand_enable) {
4791                 s = get_strip (sur->expand, get_address (msg));
4792         } else {
4793                 s = _select;
4794         }
4795         if (s) {
4796                 if (id > 0) {
4797                         --id;
4798                 }
4799                 if (s->eq_shape_controllable (id)) {
4800                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4801                         return 0;
4802                 }
4803         }
4804         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
4805 }
4806
4807 void
4808 OSC::gui_selection_changed ()
4809 {
4810         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
4811
4812         if (strip) {
4813                 _select = strip;
4814                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4815                         OSCSurface* sur = &_surface[it];
4816                         if(!sur->expand_enable) {
4817                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4818                                 _strip_select (strip, addr);
4819                         }
4820                 }
4821         }
4822 }
4823
4824 // timer callbacks
4825 bool
4826 OSC::periodic (void)
4827 {
4828         if (!tick) {
4829                 Glib::usleep(100); // let flurry of signals subside
4830                 if (global_init) {
4831                         Glib::Threads::Mutex::Lock lm (surfaces_lock);
4832                         for (uint32_t it = 0; it < _surface.size(); it++) {
4833                                 OSCSurface* sur = &_surface[it];
4834                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4835                                 global_feedback (*sur, addr);
4836                         }
4837                         global_init = false;
4838                         tick = true;
4839                 }
4840                 if (bank_dirty) {
4841                         _recalcbanks ();
4842                         bank_dirty = false;
4843                         tick = true;
4844                 }
4845         }
4846
4847         if (scrub_speed != 0) {
4848                 // for those jog wheels that don't have 0 on release (touch), time out.
4849                 int64_t now = ARDOUR::get_microseconds ();
4850                 int64_t diff = now - scrub_time;
4851                 if (diff > 120000) {
4852                         scrub_speed = 0;
4853                         session->request_transport_speed (0);
4854                         // locate to the place PH was at last tick
4855                         session->request_locate (scrub_place, false);
4856                 }
4857         }
4858
4859         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
4860
4861                 OSCGlobalObserver* go;
4862
4863                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
4864                         go->tick();
4865                 }
4866         }
4867         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
4868
4869                 OSCRouteObserver* ro;
4870
4871                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
4872                         ro->tick();
4873                 }
4874         }
4875         for (uint32_t it = 0; it < _surface.size(); it++) {
4876                 OSCSurface* sur = &_surface[it];
4877                 OSCSelectObserver* so;
4878                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
4879                         so->tick();
4880                 }
4881         }
4882         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
4883
4884                 OSCCueObserver* co;
4885
4886                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4887                         co->tick();
4888                 }
4889         }
4890         for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
4891                 _touch_timeout[(*x).first] = (*x).second - 1;
4892                 if (!(*x).second) {
4893                         boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
4894                         // turn touch off
4895                         ctrl->stop_touch (ctrl->session().transport_sample());
4896                         _touch_timeout.erase (x++);
4897                 } else {
4898                         x++;
4899                 }
4900         }
4901         return true;
4902 }
4903
4904 int
4905 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
4906 {
4907         OSCSurface *sur = get_surface(addr);
4908
4909         ostringstream os;
4910         lo_message reply;
4911         if (ssid) {
4912                 reply = lo_message_new ();
4913                 if (sur->feedback[2]) {
4914                         os << "/strip/" << path << "/" << ssid;
4915                 } else {
4916                         os << "/strip/" << path;
4917                         lo_message_add_int32 (reply, ssid);
4918                 }
4919                 string str_pth = os.str();
4920                 lo_message_add_float (reply, (float) val);
4921
4922                 lo_send_message (addr, str_pth.c_str(), reply);
4923                 lo_message_free (reply);
4924         }
4925         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
4926                 os.str("");
4927                 os << "/select/" << path;
4928                 string sel_pth = os.str();
4929                 reply = lo_message_new ();
4930                 lo_message_add_float (reply, (float) val);
4931                 lo_send_message (addr, sel_pth.c_str(), reply);
4932                 lo_message_free (reply);
4933         }
4934
4935         return 0;
4936 }
4937
4938 int
4939 OSC::sel_fail (string path, float val, lo_address addr)
4940 {
4941         ostringstream os;
4942         os.str("");
4943         os << "/select/" << path;
4944         string sel_pth = os.str();
4945         lo_message reply = lo_message_new ();
4946         lo_message_add_float (reply, (float) val);
4947         lo_send_message (addr, sel_pth.c_str(), reply);
4948         lo_message_free (reply);
4949
4950         return 0;
4951 }
4952
4953 int
4954 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
4955 {
4956         OSCSurface *sur = get_surface(addr);
4957
4958         ostringstream os;
4959         lo_message reply;
4960         reply = lo_message_new ();
4961         if (sur->feedback[2]) {
4962                 os << "/select/" << path << "/" << id;
4963         } else {
4964                 os << "/select/" << path;
4965                 lo_message_add_int32 (reply, id);
4966         }
4967         string str_pth = os.str();
4968         lo_message_add_float (reply, (float) val);
4969
4970         lo_send_message (addr, str_pth.c_str(), reply);
4971         lo_message_free (reply);
4972
4973         return 0;
4974 }
4975
4976 XMLNode&
4977 OSC::get_state ()
4978 {
4979         XMLNode& node (ControlProtocol::get_state());
4980         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
4981         node.set_property ("address-only", address_only);
4982         node.set_property ("remote-port", remote_port);
4983         node.set_property ("banksize", default_banksize);
4984         node.set_property ("striptypes", default_strip);
4985         node.set_property ("feedback", default_feedback);
4986         node.set_property ("gainmode", default_gainmode);
4987         node.set_property ("send-page-size", default_send_size);
4988         node.set_property ("plug-page-size", default_plugin_size);
4989         return node;
4990 }
4991
4992 int
4993 OSC::set_state (const XMLNode& node, int version)
4994 {
4995         if (ControlProtocol::set_state (node, version)) {
4996                 return -1;
4997         }
4998         int32_t debugmode;
4999         if (node.get_property (X_("debugmode"), debugmode)) {
5000                 _debugmode = OSCDebugMode (debugmode);
5001         }
5002
5003         node.get_property (X_("address-only"), address_only);
5004         node.get_property (X_("remote-port"), remote_port);
5005         node.get_property (X_("banksize"), default_banksize);
5006         node.get_property (X_("striptypes"), default_strip);
5007         node.get_property (X_("feedback"), default_feedback);
5008         node.get_property (X_("gainmode"), default_gainmode);
5009         node.get_property (X_("send-page-size"), default_send_size);
5010         node.get_property (X_("plugin-page-size"), default_plugin_size);
5011
5012         global_init = true;
5013         tick = false;
5014
5015         return 0;
5016 }
5017
5018 // predicate for sort call in get_sorted_stripables
5019 struct StripableByPresentationOrder
5020 {
5021         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
5022         {
5023                 return a->presentation_info().order() < b->presentation_info().order();
5024         }
5025
5026         bool operator () (const Stripable & a, const Stripable & b) const
5027         {
5028                 return a.presentation_info().order() < b.presentation_info().order();
5029         }
5030
5031         bool operator () (const Stripable * a, const Stripable * b) const
5032         {
5033                 return a->presentation_info().order() < b->presentation_info().order();
5034         }
5035 };
5036
5037 OSC::Sorted
5038 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
5039 {
5040         Sorted sorted;
5041         StripableList stripables;
5042
5043         // fetch all stripables
5044         session->get_stripables (stripables);
5045
5046         // Look for stripables that match bit in sur->strip_types
5047         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5048
5049                 boost::shared_ptr<Stripable> s = *it;
5050                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5051                         // do nothing... skip it
5052                 } else if (types[8] && (s->is_selected())) {
5053                         sorted.push_back (s);
5054                 } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5055                         sorted.push_back (s);
5056                 } else if (s->is_master() || s->is_monitor() || s->is_auditioner()) {
5057                         // do nothing for these either (we add them later)
5058                 } else {
5059                         if (types[0] && boost::dynamic_pointer_cast<AudioTrack>(s)) {
5060                                 sorted.push_back (s);
5061                         } else if (types[1] && boost::dynamic_pointer_cast<MidiTrack>(s)) {
5062                                 sorted.push_back (s);
5063                         } else if (types[4] && boost::dynamic_pointer_cast<VCA>(s)) {
5064                                 sorted.push_back (s);
5065                         } else
5066 #ifdef MIXBUS
5067                         if (types[2] && Profile->get_mixbus() && s->mixbus()) {
5068                                 sorted.push_back (s);
5069                         } else
5070                         if (types[7] && boost::dynamic_pointer_cast<Route>(s) && !boost::dynamic_pointer_cast<Track>(s)) {
5071                                 if (Profile->get_mixbus() && !s->mixbus()) {
5072                                         sorted.push_back (s);
5073                                 }
5074                         } else
5075 #endif
5076                         if ((types[2] || types[3] || types[7]) && boost::dynamic_pointer_cast<Route>(s) && !boost::dynamic_pointer_cast<Track>(s)) {
5077                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
5078                                 if (!(s->presentation_info().flags() & PresentationInfo::MidiBus)) {
5079                                         // note some older sessions will show midibuses as busses
5080                                         if (r->direct_feeds_according_to_reality (session->master_out())) {
5081                                                 // this is a bus
5082                                                 if (types[2]) {
5083                                                         sorted.push_back (s);
5084                                                 }
5085                                         } else {
5086                                                 // this is an Aux out
5087                                                 if (types[7]) {
5088                                                         sorted.push_back (s);
5089                                                 }
5090                                         }
5091                                 } else if (types[3]) {
5092                                                 sorted.push_back (s);
5093                                 }
5094                         }
5095                 }
5096         }
5097         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5098         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
5099         if (types[5]) {
5100                 sorted.push_back (session->master_out());
5101         }
5102         if (types[6]) {
5103                 if (session->monitor_out()) {
5104                         sorted.push_back (session->monitor_out());
5105                 }
5106         }
5107         return sorted;
5108 }
5109
5110 int
5111 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
5112 {
5113         int ret = 1; /* unhandled */
5114
5115         if (!strncmp (path, "/cue/aux", 8)) {
5116                 // set our Aux bus
5117                 if (argv[0]->f) {
5118                         ret = cue_set (argv[0]->f, msg);
5119                 } else {
5120                         ret = 0;
5121                 }
5122         }
5123         else if (!strncmp (path, "/cue/connect", 12)) {
5124                 // Connect to default Aux bus
5125                 if ((!argc) || argv[0]->f) {
5126                         ret = cue_set (1, msg);
5127                 } else {
5128                         ret = 0;
5129                 }
5130         }
5131         else if (!strncmp (path, "/cue/next_aux", 13)) {
5132                 // switch to next Aux bus
5133                 if ((!argc) || argv[0]->f) {
5134                         ret = cue_next (msg);
5135                 } else {
5136                         ret = 0;
5137                 }
5138         }
5139         else if (!strncmp (path, "/cue/previous_aux", 17)) {
5140                 // switch to previous Aux bus
5141                 if ((!argc) || argv[0]->f) {
5142                         ret = cue_previous (msg);
5143                 } else {
5144                         ret = 0;
5145                 }
5146         }
5147         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
5148                 int id = atoi (&path[16]);
5149                 ret = cue_send_fader (id, argv[0]->f, msg);
5150         }
5151         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
5152                 int id = atoi (&path[17]);
5153                 ret = cue_send_enable (id, argv[0]->f, msg);
5154         }
5155         else if (!strncmp (path, "/cue/fader", 10)) {
5156                 ret = cue_aux_fader (argv[0]->f, msg);
5157         }
5158         else if (!strncmp (path, "/cue/mute", 9)) {
5159                 ret = cue_aux_mute (argv[0]->f, msg);
5160         }
5161
5162         return ret;
5163 }
5164
5165 int
5166 OSC::cue_set (uint32_t aux, lo_message msg)
5167 {
5168         return _cue_set (aux, get_address (msg));
5169 }
5170
5171 int
5172 OSC::_cue_set (uint32_t aux, lo_address addr)
5173 {
5174         int ret = 1;
5175         OSCSurface *s = get_surface(addr);
5176         s->bank_size = 0;
5177         s->strip_types = 128;
5178         s->feedback = 0;
5179         s->gainmode = 1;
5180         s->cue = true;
5181         s->strips = get_sorted_stripables(s->strip_types, s->cue);
5182
5183         s->nstrips = s->strips.size();
5184
5185         if (aux < 1) {
5186                 aux = 1;
5187         } else if (aux > s->nstrips) {
5188                 aux = s->nstrips;
5189         }
5190         s->aux = aux;
5191
5192         // get rid of any old CueObsevers for this address
5193         //cueobserver_connections.drop_connections ();
5194         CueObservers::iterator x;
5195         for (x = cue_observers.begin(); x != cue_observers.end();) {
5196
5197                 OSCCueObserver* co;
5198
5199                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
5200
5201                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
5202
5203                         if (res == 0) {
5204                                 delete *x;
5205                                 x = cue_observers.erase (x);
5206                         } else {
5207                                 ++x;
5208                         }
5209                 } else {
5210                         ++x;
5211                 }
5212         }
5213
5214         // get a list of Auxes
5215         for (uint32_t n = 0; n < s->nstrips; ++n) {
5216                 boost::shared_ptr<Stripable> stp = s->strips[n];
5217                 if (stp) {
5218                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
5219                         if (aux == n+1) {
5220                                 // aux must be at least one
5221                                 // need a signal if aux vanishes
5222                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
5223
5224                                 // make a list of stripables with sends that go to this bus
5225                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
5226                                 // start cue observer
5227                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
5228                                 cue_observers.push_back (co);
5229                                 ret = 0;
5230                         }
5231
5232                 }
5233         }
5234
5235         return ret;
5236 }
5237
5238 int
5239 OSC::cue_next (lo_message msg)
5240 {
5241         OSCSurface *s = get_surface(get_address (msg));
5242         int ret = 1;
5243
5244         if (!s->cue) {
5245                 ret = cue_set (1, msg);
5246         }
5247         if (s->aux < s->nstrips) {
5248                 ret = cue_set (s->aux + 1, msg);
5249         } else {
5250                 ret = cue_set (s->nstrips, msg);
5251         }
5252         return ret;
5253 }
5254
5255 int
5256 OSC::cue_previous (lo_message msg)
5257 {
5258         OSCSurface *s = get_surface(get_address (msg));
5259         int ret = 1;
5260         if (!s->cue) {
5261                 ret = cue_set (1, msg);
5262         }
5263         if (s->aux > 1) {
5264                 ret = cue_set (s->aux - 1, msg);
5265         }
5266         return ret;
5267 }
5268
5269 boost::shared_ptr<Send>
5270 OSC::cue_get_send (uint32_t id, lo_address addr)
5271 {
5272         OSCSurface *s = get_surface(addr);
5273         if (id && s->aux > 0 && id <= s->sends.size()) {
5274                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
5275                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
5276                 if (r && aux) {
5277                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5278                 }
5279         }
5280         return boost::shared_ptr<Send>();
5281
5282 }
5283
5284 int
5285 OSC::cue_aux_fader (float position, lo_message msg)
5286 {
5287         if (!session) return -1;
5288
5289         OSCSurface *sur = get_surface(get_address (msg));
5290         if (sur->cue) {
5291                 if (sur->aux) {
5292                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5293
5294                         if (s) {
5295                                 if (s->gain_control()) {
5296                                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
5297                                         return 0;
5298                                 }
5299                         }
5300                 }
5301         }
5302         cue_float_message ("/cue/fader", 0, get_address (msg));
5303         return -1;
5304 }
5305
5306 int
5307 OSC::cue_aux_mute (float state, lo_message msg)
5308 {
5309         if (!session) return -1;
5310
5311         OSCSurface *sur = get_surface(get_address (msg));
5312         if (sur->cue) {
5313                 if (sur->aux) {
5314                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5315                         if (s) {
5316                                 if (s->mute_control()) {
5317                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
5318                                         return 0;
5319                                 }
5320                         }
5321                 }
5322         }
5323         cue_float_message ("/cue/mute", 0, get_address (msg));
5324         return -1;
5325 }
5326
5327 int
5328 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
5329 {
5330         if (!session) {
5331                 return -1;
5332         }
5333         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5334         if (s) {
5335                 if (s->gain_control()) {
5336                         s->gain_control()->set_value (s->gain_control()->interface_to_internal(val), PBD::Controllable::NoGroup);
5337                         return 0;
5338                 }
5339         }
5340         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
5341         return -1;
5342 }
5343
5344 int
5345 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
5346 {
5347         if (!session)
5348                 return -1;
5349         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5350         if (s) {
5351                 if (state) {
5352                         s->activate ();
5353                 } else {
5354                         s->deactivate ();
5355                 }
5356                 return 0;
5357         }
5358         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
5359         return -1;
5360 }
5361
5362 int
5363 OSC::cue_float_message (string path, float val, lo_address addr)
5364 {
5365
5366         lo_message reply;
5367         reply = lo_message_new ();
5368         lo_message_add_float (reply, (float) val);
5369
5370         lo_send_message (addr, path.c_str(), reply);
5371         lo_message_free (reply);
5372
5373         return 0;
5374 }
5375
5376 int
5377 OSC::text_message (string path, string val, lo_address addr)
5378 {
5379
5380         lo_message reply;
5381         reply = lo_message_new ();
5382         lo_message_add_string (reply, val.c_str());
5383
5384         lo_send_message (addr, path.c_str(), reply);
5385         lo_message_free (reply);
5386
5387         return 0;
5388 }
5389
5390
5391 // we have to have a sorted list of stripables that have sends pointed at our aux
5392 // we can use the one in osc.cc to get an aux list
5393 OSC::Sorted
5394 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
5395 {
5396         Sorted sorted;
5397         cueobserver_connections.drop_connections ();
5398         // fetch all stripables
5399         StripableList stripables;
5400
5401         session->get_stripables (stripables);
5402
5403         // Look for stripables that have a send to aux
5404         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5405
5406                 boost::shared_ptr<Stripable> s = *it;
5407                 // we only want routes
5408                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5409                 if (r) {
5410                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
5411                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5412                         if (snd) { // test for send to aux
5413                                 sorted.push_back (s);
5414                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
5415                         }
5416                 }
5417
5418
5419         }
5420         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5421
5422         return sorted;
5423 }
5424