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