OSC: update /strip/list response to add aux, midi bus and VCAs
[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         }
1510 }
1511
1512 /*
1513  * This gets called not only when bank changes but also:
1514  *  - bank size change
1515  *  - feedback change
1516  *  - strip types changes
1517  *  - fadermode changes
1518  *  - stripable creation/deletion/flag
1519  *  - to refresh what is "displayed"
1520  * Basically any time the bank needs to be rebuilt
1521  */
1522 int
1523 OSC::set_bank (uint32_t bank_start, lo_message msg)
1524 {
1525         return _set_bank (bank_start, get_address (msg));
1526 }
1527
1528 // set bank is callable with either message or address
1529 int
1530 OSC::_set_bank (uint32_t bank_start, lo_address addr)
1531 {
1532         if (!session) {
1533                 return -1;
1534         }
1535         // no nstripables yet
1536         if (!session->nroutes()) {
1537                 return -1;
1538         }
1539
1540         OSCSurface *s = get_surface (addr);
1541
1542         // revert any expand to select
1543          s->expand = 0;
1544          s->expand_enable = false;
1545         _strip_select (ControlProtocol::first_selected_stripable(), addr);
1546
1547         // undo all listeners for this url
1548         StripableList stripables;
1549         session->get_stripables (stripables);
1550         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
1551
1552                 boost::shared_ptr<Stripable> stp = *it;
1553                 if (stp) {
1554                         end_listen (stp, addr);
1555                 }
1556                 // slow devices need time to clear buffers
1557                 usleep ((uint32_t) 10);
1558         }
1559
1560         s->strips = get_sorted_stripables(s->strip_types, s->cue);
1561         s->nstrips = s->strips.size();
1562
1563         uint32_t b_size;
1564         if (!s->bank_size) {
1565                 // no banking - bank includes all stripables
1566                 b_size = s->nstrips;
1567         } else {
1568                 b_size = s->bank_size;
1569         }
1570
1571         // Do limits checking
1572         if (bank_start < 1) bank_start = 1;
1573         if (b_size >= s->nstrips)  {
1574                 bank_start = 1;
1575         } else if (bank_start > ((s->nstrips - b_size) + 1)) {
1576                 // top bank is always filled if there are enough strips for at least one bank
1577                 bank_start = (uint32_t)((s->nstrips - b_size) + 1);
1578         }
1579         //save bank in case we have had to change it
1580         s->bank = bank_start;
1581
1582         if (s->feedback[0] || s->feedback[1]) {
1583
1584                 for (uint32_t n = bank_start; n < (min ((b_size + bank_start), s->nstrips + 1)); ++n) {
1585                         if (n <= s->strips.size()) {
1586                                 boost::shared_ptr<Stripable> stp = s->strips[n - 1];
1587
1588                                 if (stp) {
1589                                         listen_to_route(stp, addr);
1590                                 }
1591                         }
1592                         // slow devices need time to clear buffers
1593                         usleep ((uint32_t) 10);
1594                 }
1595         }
1596         // light bankup or bankdown buttons if it is possible to bank in that direction
1597         if (s->feedback[4]) {
1598                 // these two messages could be bundled
1599                 lo_message reply;
1600                 reply = lo_message_new ();
1601                 if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
1602                         lo_message_add_int32 (reply, 0);
1603                 } else {
1604                         lo_message_add_int32 (reply, 1);
1605                 }
1606                 lo_send_message (addr, "/bank_up", reply);
1607                 lo_message_free (reply);
1608                 reply = lo_message_new ();
1609                 if (s->bank > 1) {
1610                         lo_message_add_int32 (reply, 1);
1611                 } else {
1612                         lo_message_add_int32 (reply, 0);
1613                 }
1614                 lo_send_message (addr, "/bank_down", reply);
1615                 lo_message_free (reply);
1616         }
1617         bank_dirty = false;
1618         tick = true;
1619         return 0;
1620 }
1621
1622 int
1623 OSC::bank_up (lo_message msg)
1624 {
1625         if (!session) {
1626                 return -1;
1627         }
1628         OSCSurface *s = get_surface(get_address (msg));
1629         set_bank (s->bank + s->bank_size, msg);
1630         return 0;
1631 }
1632
1633 int
1634 OSC::bank_down (lo_message msg)
1635 {
1636         if (!session) {
1637                 return -1;
1638         }
1639         OSCSurface *s = get_surface(get_address (msg));
1640         if (s->bank < s->bank_size) {
1641                 set_bank (1, msg);
1642         } else {
1643                 set_bank (s->bank - s->bank_size, msg);
1644         }
1645         return 0;
1646 }
1647
1648 uint32_t
1649 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
1650 {
1651         if (!strip) {
1652                 return 0;
1653         }
1654
1655         OSCSurface *s = get_surface(addr);
1656
1657         uint32_t b_size;
1658         if (!s->bank_size) {
1659                 // no banking
1660                 b_size = s->nstrips;
1661         } else {
1662                 b_size = s->bank_size;
1663         }
1664
1665         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
1666                 if (n <= s->strips.size()) {
1667                         if (strip == s->strips[n-1]) {
1668                                 return n - s->bank + 1;
1669                         }
1670                 }
1671         }
1672         // failsafe... should never get here.
1673         return 0;
1674 }
1675
1676 boost::shared_ptr<ARDOUR::Stripable>
1677 OSC::get_strip (uint32_t ssid, lo_address addr)
1678 {
1679         OSCSurface *s = get_surface(addr);
1680         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
1681                 return s->strips[ssid + s->bank - 2];
1682         }
1683         // guess it is out of range
1684         return boost::shared_ptr<ARDOUR::Stripable>();
1685 }
1686
1687 void
1688 OSC::transport_frame (lo_message msg)
1689 {
1690         if (!session) {
1691                 return;
1692         }
1693         framepos_t pos = session->transport_frame ();
1694
1695         lo_message reply = lo_message_new ();
1696         lo_message_add_int64 (reply, pos);
1697
1698         lo_send_message (get_address (msg), "/transport_frame", reply);
1699
1700         lo_message_free (reply);
1701 }
1702
1703 void
1704 OSC::transport_speed (lo_message msg)
1705 {
1706         if (!session) {
1707                 return;
1708         }
1709         double ts = session->transport_speed ();
1710
1711         lo_message reply = lo_message_new ();
1712         lo_message_add_double (reply, ts);
1713
1714         lo_send_message (get_address (msg), "/transport_speed", reply);
1715
1716         lo_message_free (reply);
1717 }
1718
1719 void
1720 OSC::record_enabled (lo_message msg)
1721 {
1722         if (!session) {
1723                 return;
1724         }
1725         int re = (int)session->get_record_enabled ();
1726
1727         lo_message reply = lo_message_new ();
1728         lo_message_add_int32 (reply, re);
1729
1730         lo_send_message (get_address (msg), "/record_enabled", reply);
1731
1732         lo_message_free (reply);
1733 }
1734
1735 // master and monitor calls
1736 int
1737 OSC::master_set_gain (float dB)
1738 {
1739         if (!session) return -1;
1740         boost::shared_ptr<Stripable> s = session->master_out();
1741         if (s) {
1742                 if (dB < -192) {
1743                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1744                 } else {
1745                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1746                 }
1747         }
1748         return 0;
1749 }
1750
1751 int
1752 OSC::master_set_fader (float position)
1753 {
1754         if (!session) return -1;
1755         boost::shared_ptr<Stripable> s = session->master_out();
1756         if (s) {
1757                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1758         }
1759         return 0;
1760 }
1761
1762 int
1763 OSC::master_set_trim (float dB)
1764 {
1765         if (!session) return -1;
1766         boost::shared_ptr<Stripable> s = session->master_out();
1767
1768         if (s) {
1769                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1770         }
1771
1772         return 0;
1773 }
1774
1775 int
1776 OSC::master_set_pan_stereo_position (float position, lo_message msg)
1777 {
1778         if (!session) return -1;
1779
1780         float endposition = .5;
1781         boost::shared_ptr<Stripable> s = session->master_out();
1782
1783         if (s) {
1784                 if (s->pan_azimuth_control()) {
1785                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
1786                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
1787                 }
1788         }
1789         OSCSurface *sur = get_surface(get_address (msg));
1790
1791         if (sur->feedback[4]) {
1792                 lo_message reply = lo_message_new ();
1793                 lo_message_add_float (reply, endposition);
1794
1795                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
1796                 lo_message_free (reply);
1797         }
1798
1799         return 0;
1800 }
1801
1802 int
1803 OSC::master_set_mute (uint32_t state)
1804 {
1805         if (!session) return -1;
1806
1807         boost::shared_ptr<Stripable> s = session->master_out();
1808
1809         if (s) {
1810                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
1811         }
1812
1813         return 0;
1814 }
1815
1816 int
1817 OSC::monitor_set_gain (float dB)
1818 {
1819         if (!session) return -1;
1820         boost::shared_ptr<Stripable> s = session->monitor_out();
1821
1822         if (s) {
1823                 if (dB < -192) {
1824                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1825                 } else {
1826                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1827                 }
1828         }
1829         return 0;
1830 }
1831
1832 int
1833 OSC::monitor_set_fader (float position)
1834 {
1835         if (!session) return -1;
1836         boost::shared_ptr<Stripable> s = session->monitor_out();
1837         if (s) {
1838                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1839         }
1840         return 0;
1841 }
1842
1843 int
1844 OSC::monitor_set_mute (uint32_t state)
1845 {
1846         if (!session) return -1;
1847
1848         if (session->monitor_out()) {
1849                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
1850                 mon->set_cut_all (state);
1851         }
1852         return 0;
1853 }
1854
1855 int
1856 OSC::monitor_set_dim (uint32_t state)
1857 {
1858         if (!session) return -1;
1859
1860         if (session->monitor_out()) {
1861                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
1862                 mon->set_dim_all (state);
1863         }
1864         return 0;
1865 }
1866
1867 int
1868 OSC::monitor_set_mono (uint32_t state)
1869 {
1870         if (!session) return -1;
1871
1872         if (session->monitor_out()) {
1873                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
1874                 mon->set_mono (state);
1875         }
1876         return 0;
1877 }
1878
1879 int
1880 OSC::route_get_sends(lo_message msg) {
1881         if (!session) {
1882                 return -1;
1883         }
1884
1885         lo_arg **argv = lo_message_get_argv(msg);
1886
1887         int rid = argv[0]->i;
1888
1889         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
1890         if (!strip) {
1891                 return -1;
1892         }
1893
1894         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
1895         if (!r) {
1896                 return -1;
1897         }
1898
1899         lo_message reply = lo_message_new();
1900         lo_message_add_int32(reply, rid);
1901
1902         int i = 0;
1903         for (;;) {
1904                 boost::shared_ptr<Processor> p = r->nth_send(i++);
1905
1906                 if (!p) {
1907                         break;
1908                 }
1909
1910                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
1911                 if (isend) {
1912                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
1913                         lo_message_add_string(reply, isend->name().c_str());
1914                         lo_message_add_int32(reply, i);
1915                         boost::shared_ptr<Amp> a = isend->amp();
1916                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
1917                         lo_message_add_int32(reply, p->active() ? 1 : 0);
1918                 }
1919         }
1920         // if used dedicated message path to identify this reply in async operation. Naming it #reply wont help the client to identify the content.
1921         lo_send_message(get_address (msg), "/strip/sends", reply);
1922
1923         lo_message_free(reply);
1924
1925         return 0;
1926 }
1927
1928 int
1929 OSC::route_get_receives(lo_message msg) {
1930         if (!session) {
1931                 return -1;
1932         }
1933
1934         lo_arg **argv = lo_message_get_argv(msg);
1935
1936         uint32_t rid = argv[0]->i;
1937
1938
1939         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
1940         if (!strip) {
1941                 return -1;
1942         }
1943
1944         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
1945         if (!r) {
1946                 return -1;
1947         }
1948
1949         boost::shared_ptr<RouteList> route_list = session->get_routes();
1950
1951         lo_message reply = lo_message_new();
1952
1953         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
1954                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
1955                 if (!tr) {
1956                         continue;
1957                 }
1958                 int j = 0;
1959
1960                 for (;;) {
1961                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
1962
1963                         if (!p) {
1964                                 break;
1965                         }
1966
1967                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
1968                         if (isend) {
1969                                 if( isend->target_route()->id() == r->id()){
1970                                         boost::shared_ptr<Amp> a = isend->amp();
1971
1972                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
1973                                         lo_message_add_string(reply, tr->name().c_str());
1974                                         lo_message_add_int32(reply, j);
1975                                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
1976                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
1977                                 }
1978                         }
1979                 }
1980         }
1981
1982         // 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.
1983         lo_send_message(get_address (msg), "/strip/receives", reply);
1984         lo_message_free(reply);
1985         return 0;
1986 }
1987
1988 // strip calls
1989 int
1990 OSC::route_mute (int ssid, int yn, lo_message msg)
1991 {
1992         if (!session) return -1;
1993         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1994
1995         if (s) {
1996                 if (s->mute_control()) {
1997                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1998                         return 0;
1999                 }
2000         }
2001
2002         return route_send_fail ("mute", ssid, 0, get_address (msg));
2003 }
2004
2005 int
2006 OSC::sel_mute (uint32_t yn, lo_message msg)
2007 {
2008         OSCSurface *sur = get_surface(get_address (msg));
2009         boost::shared_ptr<Stripable> s;
2010         if (sur->expand_enable) {
2011                 s = get_strip (sur->expand, get_address (msg));
2012         } else {
2013                 s = _select;
2014         }
2015         if (s) {
2016                 if (s->mute_control()) {
2017                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2018                         return 0;
2019                 }
2020         }
2021         return sel_fail ("mute", 0, get_address (msg));
2022 }
2023
2024 int
2025 OSC::route_solo (int ssid, int yn, lo_message msg)
2026 {
2027         if (!session) return -1;
2028         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2029
2030         if (s) {
2031                 if (s->solo_control()) {
2032                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2033                 }
2034         }
2035
2036         return route_send_fail ("solo", ssid, 0, get_address (msg));
2037 }
2038
2039 int
2040 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2041 {
2042         if (!session) return -1;
2043         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2044
2045         if (s) {
2046                 if (s->solo_isolate_control()) {
2047                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2048                         return 0;
2049                 }
2050         }
2051
2052         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2053 }
2054
2055 int
2056 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2057 {
2058         if (!session) return -1;
2059         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2060
2061         if (s) {
2062                 if (s->solo_safe_control()) {
2063                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2064                         return 0;
2065                 }
2066         }
2067
2068         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2069 }
2070
2071 int
2072 OSC::sel_solo (uint32_t yn, lo_message msg)
2073 {
2074         OSCSurface *sur = get_surface(get_address (msg));
2075         boost::shared_ptr<Stripable> s;
2076         if (sur->expand_enable) {
2077                 s = get_strip (sur->expand, get_address (msg));
2078         } else {
2079                 s = _select;
2080         }
2081         if (s) {
2082                 if (s->solo_control()) {
2083                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2084                 }
2085         }
2086         return sel_fail ("solo", 0, get_address (msg));
2087 }
2088
2089 int
2090 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2091 {
2092         OSCSurface *sur = get_surface(get_address (msg));
2093         boost::shared_ptr<Stripable> s;
2094         if (sur->expand_enable) {
2095                 s = get_strip (sur->expand, get_address (msg));
2096         } else {
2097                 s = _select;
2098         }
2099         if (s) {
2100                 if (s->solo_isolate_control()) {
2101                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2102                         return 0;
2103                 }
2104         }
2105         return sel_fail ("solo_iso", 0, get_address (msg));
2106 }
2107
2108 int
2109 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2110 {
2111         OSCSurface *sur = get_surface(get_address (msg));
2112         boost::shared_ptr<Stripable> s;
2113         if (sur->expand_enable) {
2114                 s = get_strip (sur->expand, get_address (msg));
2115         } else {
2116                 s = _select;
2117         }
2118         if (s) {
2119                 if (s->solo_safe_control()) {
2120                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2121                         return 0;
2122                 }
2123         }
2124         return sel_fail ("solo_safe", 0, get_address (msg));
2125 }
2126
2127 int
2128 OSC::sel_recenable (uint32_t yn, lo_message msg)
2129 {
2130         OSCSurface *sur = get_surface(get_address (msg));
2131         boost::shared_ptr<Stripable> s;
2132         if (sur->expand_enable) {
2133                 s = get_strip (sur->expand, get_address (msg));
2134         } else {
2135                 s = _select;
2136         }
2137         if (s) {
2138                 if (s->rec_enable_control()) {
2139                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2140                         if (s->rec_enable_control()->get_value()) {
2141                                 return 0;
2142                         }
2143                 }
2144         }
2145         return sel_fail ("recenable", 0, get_address (msg));
2146 }
2147
2148 int
2149 OSC::route_recenable (int ssid, int yn, lo_message msg)
2150 {
2151         if (!session) return -1;
2152         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2153
2154         if (s) {
2155                 if (s->rec_enable_control()) {
2156                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2157                         if (s->rec_enable_control()->get_value()) {
2158                                 return 0;
2159                         }
2160                 }
2161         }
2162         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2163 }
2164
2165 int
2166 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2167     if (!session) {
2168         return -1;
2169     }
2170
2171     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2172
2173     if (s) {
2174         s->set_name(std::string(newname));
2175     }
2176
2177     return 0;
2178 }
2179
2180 int
2181 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2182 {
2183         OSCSurface *sur = get_surface(get_address (msg));
2184         boost::shared_ptr<Stripable> s;
2185         if (sur->expand_enable) {
2186                 s = get_strip (sur->expand, get_address (msg));
2187         } else {
2188                 s = _select;
2189         }
2190         if (s) {
2191                 if (s->rec_safe_control()) {
2192                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2193                         if (s->rec_safe_control()->get_value()) {
2194                                 return 0;
2195                         }
2196                 }
2197         }
2198         return sel_fail ("record_safe", 0, get_address (msg));
2199 }
2200
2201 int
2202 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2203 {
2204         if (!session) return -1;
2205         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2206         if (s) {
2207                 if (s->rec_safe_control()) {
2208                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2209                         if (s->rec_safe_control()->get_value()) {
2210                                 return 0;
2211                         }
2212                 }
2213         }
2214         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2215 }
2216
2217 int
2218 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2219 {
2220         if (!session) return -1;
2221         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2222
2223         if (s) {
2224                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2225                 if (track) {
2226                         if (track->monitoring_control()) {
2227                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2228                                 return 0;
2229                         }
2230                 }
2231         }
2232
2233         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2234 }
2235
2236 int
2237 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2238 {
2239         OSCSurface *sur = get_surface(get_address (msg));
2240         boost::shared_ptr<Stripable> s;
2241         if (sur->expand_enable) {
2242                 s = get_strip (sur->expand, get_address (msg));
2243         } else {
2244                 s = _select;
2245         }
2246         if (s) {
2247                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2248                 if (track) {
2249                         if (track->monitoring_control()) {
2250                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2251                                 return 0;
2252                         }
2253                 }
2254         }
2255         return sel_fail ("monitor_input", 0, get_address (msg));
2256 }
2257
2258 int
2259 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2260 {
2261         if (!session) return -1;
2262         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2263
2264         if (s) {
2265                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2266                 if (track) {
2267                         if (track->monitoring_control()) {
2268                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2269                                 return 0;
2270                         }
2271                 }
2272         }
2273
2274         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2275 }
2276
2277 int
2278 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2279 {
2280         OSCSurface *sur = get_surface(get_address (msg));
2281         boost::shared_ptr<Stripable> s;
2282         if (sur->expand_enable) {
2283                 s = get_strip (sur->expand, get_address (msg));
2284         } else {
2285                 s = _select;
2286         }
2287         if (s) {
2288                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2289                 if (track) {
2290                         if (track->monitoring_control()) {
2291                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2292                                 return 0;
2293                         }
2294                 }
2295         }
2296         return sel_fail ("monitor_disk", 0, get_address (msg));
2297 }
2298
2299
2300 int
2301 OSC::strip_phase (int ssid, int yn, lo_message msg)
2302 {
2303         if (!session) return -1;
2304         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2305
2306         if (s) {
2307                 if (s->phase_control()) {
2308                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2309                         return 0;
2310                 }
2311         }
2312
2313         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2314 }
2315
2316 int
2317 OSC::sel_phase (uint32_t yn, lo_message msg)
2318 {
2319         OSCSurface *sur = get_surface(get_address (msg));
2320         boost::shared_ptr<Stripable> s;
2321         if (sur->expand_enable) {
2322                 s = get_strip (sur->expand, get_address (msg));
2323         } else {
2324                 s = _select;
2325         }
2326         if (s) {
2327                 if (s->phase_control()) {
2328                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2329                         return 0;
2330                 }
2331         }
2332         return sel_fail ("polarity", 0, get_address (msg));
2333 }
2334
2335 int
2336 OSC::strip_expand (int ssid, int yn, lo_message msg)
2337 {
2338         OSCSurface *sur = get_surface(get_address (msg));
2339         sur->expand_enable = (bool) yn;
2340         sur->expand = ssid;
2341         boost::shared_ptr<Stripable> s;
2342         if (yn) {
2343                 s = get_strip (ssid, get_address (msg));
2344         } else {
2345                 s = ControlProtocol::first_selected_stripable();
2346         }
2347
2348         return _strip_select (s, get_address (msg));
2349 }
2350
2351 int
2352 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2353 {
2354         if (!session) {
2355                 return -1;
2356         }
2357         OSCSurface *sur = get_surface(addr);
2358         if (sur->sel_obs) {
2359                 delete sur->sel_obs;
2360                 sur->sel_obs = 0;
2361         }
2362         bool feedback_on = sur->feedback.to_ulong();
2363         if (s && feedback_on) {
2364                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2365                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2366                 sur->sel_obs = sel_fb;
2367         } else if (sur->expand_enable) {
2368                 sur->expand = 0;
2369                 sur->expand_enable = false;
2370                 if (_select && feedback_on) {
2371                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2372                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2373                         sur->sel_obs = sel_fb;
2374                 }
2375         } else if (feedback_on) {
2376                 route_send_fail ("select", sur->expand, 0 , addr);
2377         }
2378         if (!feedback_on) {
2379                 return 0;
2380         }
2381         //update buttons on surface
2382         int b_s = sur->bank_size;
2383         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2384                 b_s = sur->nstrips;
2385         }
2386         for (int i = 1;  i <= b_s; i++) {
2387                 string path = "expand";
2388
2389                 if ((i == (int) sur->expand) && sur->expand_enable) {
2390                         lo_message reply = lo_message_new ();
2391                         if (sur->feedback[2]) {
2392                                 ostringstream os;
2393                                 os << "/strip/" << path << "/" << i;
2394                                 path = os.str();
2395                         } else {
2396                                 ostringstream os;
2397                                 os << "/strip/" << path;
2398                                 path = os.str();
2399                                 lo_message_add_int32 (reply, i);
2400                         }
2401                         lo_message_add_float (reply, (float) 1);
2402
2403                         lo_send_message (addr, path.c_str(), reply);
2404                         lo_message_free (reply);
2405                         reply = lo_message_new ();
2406                         lo_message_add_float (reply, 1.0);
2407                         lo_send_message (addr, "/select/expand", reply);
2408                         lo_message_free (reply);
2409
2410                 } else {
2411                         lo_message reply = lo_message_new ();
2412                         lo_message_add_int32 (reply, i);
2413                         lo_message_add_float (reply, 0.0);
2414                         lo_send_message (addr, "/strip/expand", reply);
2415                         lo_message_free (reply);
2416                 }
2417         }
2418         if (!sur->expand_enable) {
2419                 lo_message reply = lo_message_new ();
2420                 lo_message_add_float (reply, 0.0);
2421                 lo_send_message (addr, "/select/expand", reply);
2422                 lo_message_free (reply);
2423         }
2424
2425         return 0;
2426 }
2427
2428 int
2429 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2430 {
2431         //ignore button release
2432         if (!yn) return 0;
2433
2434         if (!session) {
2435                 return -1;
2436         }
2437         OSCSurface *sur = get_surface(get_address (msg));
2438         sur->expand_enable = false;
2439         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2440         if (s) {
2441                 SetStripableSelection (s);
2442         } else {
2443                 if ((int) (sur->feedback.to_ulong())) {
2444                         route_send_fail ("select", ssid, 0, get_address (msg));
2445                 }
2446         }
2447
2448         return 0;
2449 }
2450
2451 int
2452 OSC::sel_expand (uint32_t state, lo_message msg)
2453 {
2454         OSCSurface *sur = get_surface(get_address (msg));
2455         boost::shared_ptr<Stripable> s;
2456         sur->expand_enable = (bool) state;
2457         if (state && sur->expand) {
2458                 s = get_strip (sur->expand, get_address (msg));
2459         } else {
2460                 s = ControlProtocol::first_selected_stripable();
2461         }
2462
2463         return _strip_select (s, get_address (msg));
2464 }
2465
2466 int
2467 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2468 {
2469         if (!session) return -1;
2470         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2471
2472         if (s) {
2473                 if (s->gain_control()) {
2474                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2475                 } else {
2476                         return 1;
2477                 }
2478         } else {
2479                 return 1;
2480         }
2481
2482         return 0;
2483 }
2484
2485 int
2486 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2487 {
2488         if (!session) {
2489                 route_send_fail ("gain", ssid, -193, get_address (msg));
2490                 return -1;
2491         }
2492         int ret;
2493         if (dB < -192) {
2494                 ret = route_set_gain_abs (ssid, 0.0, msg);
2495         } else {
2496                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2497         }
2498         if (ret != 0) {
2499                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2500         }
2501         return 0;
2502 }
2503
2504 int
2505 OSC::sel_gain (float val, lo_message msg)
2506 {
2507         OSCSurface *sur = get_surface(get_address (msg));
2508         boost::shared_ptr<Stripable> s;
2509         if (sur->expand_enable) {
2510                 s = get_strip (sur->expand, get_address (msg));
2511         } else {
2512                 s = _select;
2513         }
2514         if (s) {
2515                 float abs;
2516                 if (val < -192) {
2517                         abs = 0;
2518                 } else {
2519                         abs = dB_to_coefficient (val);
2520                 }
2521                 if (s->gain_control()) {
2522                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2523                         return 0;
2524                 }
2525         }
2526         return sel_fail ("gain", -193, get_address (msg));
2527 }
2528
2529 int
2530 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2531 {
2532         if (!session) {
2533                 route_send_fail ("fader", ssid, 0, get_address (msg));
2534                 return -1;
2535         }
2536         int ret;
2537         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2538         if (ret != 0) {
2539                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2540         }
2541         return 0;
2542 }
2543
2544 int
2545 OSC::sel_fader (float val, lo_message msg)
2546 {
2547         OSCSurface *sur = get_surface(get_address (msg));
2548         boost::shared_ptr<Stripable> s;
2549         if (sur->expand_enable) {
2550                 s = get_strip (sur->expand, get_address (msg));
2551         } else {
2552                 s = _select;
2553         }
2554         if (s) {
2555                 float abs;
2556                 abs = slider_position_to_gain_with_max (val, 2.0);
2557                 if (s->gain_control()) {
2558                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2559                         return 0;
2560                 }
2561         }
2562         return sel_fail ("fader", 0, get_address (msg));
2563 }
2564
2565 int
2566 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2567 {
2568         if (!session) return -1;
2569         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2570
2571         if (s) {
2572                 if (s->trim_control()) {
2573                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2574                         return 0;
2575                 }
2576
2577         }
2578
2579         return -1;
2580 }
2581
2582 int
2583 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2584 {
2585         int ret;
2586         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2587         if (ret != 0) {
2588                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2589         }
2590
2591 return 0;
2592 }
2593
2594 int
2595 OSC::sel_trim (float val, lo_message msg)
2596 {
2597         OSCSurface *sur = get_surface(get_address (msg));
2598         boost::shared_ptr<Stripable> s;
2599         if (sur->expand_enable) {
2600                 s = get_strip (sur->expand, get_address (msg));
2601         } else {
2602                 s = _select;
2603         }
2604         if (s) {
2605                 if (s->trim_control()) {
2606                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2607                         return 0;
2608                 }
2609         }
2610         return sel_fail ("trimdB", 0, get_address (msg));
2611 }
2612
2613 int
2614 OSC::sel_pan_position (float val, lo_message msg)
2615 {
2616         OSCSurface *sur = get_surface(get_address (msg));
2617         boost::shared_ptr<Stripable> s;
2618         if (sur->expand_enable) {
2619                 s = get_strip (sur->expand, get_address (msg));
2620         } else {
2621                 s = _select;
2622         }
2623         if (s) {
2624                 if(s->pan_azimuth_control()) {
2625                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2626                         return 0;
2627                 }
2628         }
2629         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2630 }
2631
2632 int
2633 OSC::sel_pan_width (float val, lo_message msg)
2634 {
2635         OSCSurface *sur = get_surface(get_address (msg));
2636         boost::shared_ptr<Stripable> s;
2637         if (sur->expand_enable) {
2638                 s = get_strip (sur->expand, get_address (msg));
2639         } else {
2640                 s = _select;
2641         }
2642         if (s) {
2643                 if (s->pan_width_control()) {
2644                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2645                         return 0;
2646                 }
2647         }
2648         return sel_fail ("pan_stereo_width", 1, get_address (msg));
2649 }
2650
2651 int
2652 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
2653 {
2654         if (!session) return -1;
2655         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2656
2657         if (s) {
2658                 if(s->pan_azimuth_control()) {
2659                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
2660                         return 0;
2661                 }
2662         }
2663
2664         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
2665 }
2666
2667 int
2668 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
2669 {
2670         if (!session) return -1;
2671         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2672
2673         if (s) {
2674                 if (s->pan_width_control()) {
2675                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
2676                         return 0;
2677                 }
2678         }
2679
2680         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
2681 }
2682
2683 int
2684 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
2685 {
2686         if (!session) {
2687                 return -1;
2688         }
2689         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2690         float abs;
2691         if (s) {
2692                 if (id > 0) {
2693                         --id;
2694                 }
2695 #ifdef MIXBUS
2696                 abs = val;
2697 #else
2698                 if (val < -192) {
2699                         abs = 0;
2700                 } else {
2701                         abs = dB_to_coefficient (val);
2702                 }
2703 #endif
2704                 if (s->send_level_controllable (id)) {
2705                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2706                         return 0;
2707                 }
2708         }
2709         return 0;
2710 }
2711
2712 int
2713 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
2714 {
2715         if (!session) {
2716                 return -1;
2717         }
2718         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2719         float abs;
2720         if (s) {
2721
2722                 if (id > 0) {
2723                         --id;
2724                 }
2725
2726                 if (s->send_level_controllable (id)) {
2727 #ifdef MIXBUS
2728                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2729 #else
2730                         abs = slider_position_to_gain_with_max (val, 2.0);
2731 #endif
2732                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2733                         return 0;
2734                 }
2735         }
2736         return 0;
2737 }
2738
2739 int
2740 OSC::sel_sendgain (int id, float val, lo_message msg)
2741 {
2742         OSCSurface *sur = get_surface(get_address (msg));
2743         boost::shared_ptr<Stripable> s;
2744         if (sur->expand_enable) {
2745                 s = get_strip (sur->expand, get_address (msg));
2746         } else {
2747                 s = _select;
2748         }
2749         float abs;
2750         if (s) {
2751                 if (id > 0) {
2752                         --id;
2753                 }
2754 #ifdef MIXBUS
2755                 abs = val;
2756 #else
2757                 if (val < -192) {
2758                         abs = 0;
2759                 } else {
2760                         abs = dB_to_coefficient (val);
2761                 }
2762 #endif
2763                 if (s->send_level_controllable (id)) {
2764                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2765                         return 0;
2766                 }
2767         }
2768         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
2769 }
2770
2771 int
2772 OSC::sel_sendfader (int id, float val, lo_message msg)
2773 {
2774         OSCSurface *sur = get_surface(get_address (msg));
2775         boost::shared_ptr<Stripable> s;
2776         if (sur->expand_enable) {
2777                 s = get_strip (sur->expand, get_address (msg));
2778         } else {
2779                 s = _select;
2780         }
2781         float abs;
2782         if (s) {
2783
2784                 if (id > 0) {
2785                         --id;
2786                 }
2787
2788                 if (s->send_level_controllable (id)) {
2789 #ifdef MIXBUS
2790                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2791 #else
2792                         abs = slider_position_to_gain_with_max (val, 2.0);
2793 #endif
2794                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2795                         return 0;
2796                 }
2797         }
2798         return sel_send_fail ("send_fader", id, 0, get_address (msg));
2799 }
2800
2801 int
2802 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
2803 {
2804         if (!session) {
2805                 return -1;
2806         }
2807         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2808
2809         if (s) {
2810
2811                 /* revert to zero-based counting */
2812
2813                 if (sid > 0) {
2814                         --sid;
2815                 }
2816
2817                 if (s->send_enable_controllable (sid)) {
2818                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
2819                         return 0;
2820                 }
2821
2822                 if (s->send_level_controllable (sid)) {
2823                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2824                         if (!r) {
2825                                 return 0;
2826                         }
2827                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
2828                         if (snd) {
2829                                 if (val) {
2830                                         snd->activate();
2831                                 } else {
2832                                         snd->deactivate();
2833                                 }
2834                         }
2835                         return 0;
2836                 }
2837
2838         }
2839
2840         return -1;
2841 }
2842
2843 int
2844 OSC::sel_sendenable (int id, float val, lo_message msg)
2845 {
2846         OSCSurface *sur = get_surface(get_address (msg));
2847         boost::shared_ptr<Stripable> s;
2848         if (sur->expand_enable) {
2849                 s = get_strip (sur->expand, get_address (msg));
2850         } else {
2851                 s = _select;
2852         }
2853         if (s) {
2854                 if (id > 0) {
2855                         --id;
2856                 }
2857                 if (s->send_enable_controllable (id)) {
2858                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
2859                         return 0;
2860                 }
2861                 if (s->send_level_controllable (id)) {
2862                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2863                         if (!r) {
2864                                 // should never get here
2865                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
2866                         }
2867                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
2868                         if (snd) {
2869                                 if (val) {
2870                                         snd->activate();
2871                                 } else {
2872                                         snd->deactivate();
2873                                 }
2874                         }
2875                         return 0;
2876                 }
2877         }
2878         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
2879 }
2880
2881 int
2882 OSC::route_plugin_list (int ssid, lo_message msg) {
2883         if (!session) {
2884                 return -1;
2885         }
2886
2887         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
2888
2889         if (!r) {
2890                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2891                 return -1;
2892         }
2893         int piid = 0;
2894
2895         lo_message reply = lo_message_new ();
2896         lo_message_add_int32 (reply, ssid);
2897
2898
2899         for (;;) {
2900                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
2901                 if ( !redi ) {
2902                         break;
2903                 }
2904
2905                 boost::shared_ptr<PluginInsert> pi;
2906
2907                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2908                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2909                         continue;
2910                 }
2911                 lo_message_add_int32 (reply, piid + 1);
2912
2913                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2914                 lo_message_add_string (reply, pip->name());
2915
2916                 piid++;
2917         }
2918
2919         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
2920         lo_message_free (reply);
2921         return 0;
2922 }
2923
2924 int
2925 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
2926         if (!session) {
2927                 return -1;
2928         }
2929
2930         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
2931
2932         if (!r) {
2933                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2934                 return -1;
2935         }
2936
2937         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
2938
2939         if (!redi) {
2940                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2941                 return -1;
2942         }
2943
2944         boost::shared_ptr<PluginInsert> pi;
2945
2946         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2947                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2948                 return -1;
2949         }
2950
2951         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2952         bool ok = false;
2953
2954         lo_message reply = lo_message_new();
2955         lo_message_add_int32 (reply, ssid);
2956         lo_message_add_int32 (reply, piid);
2957         lo_message_add_string (reply, pip->name());
2958         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
2959
2960                 uint32_t controlid = pip->nth_parameter(ppi, ok);
2961                 if (!ok) {
2962                         continue;
2963                 }
2964                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
2965                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
2966
2967                                 lo_message_add_int32 (reply, ppi + 1);
2968                                 ParameterDescriptor pd;
2969                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
2970                                 lo_message_add_string (reply, pd.label.c_str());
2971
2972                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
2973                                 int flags = 0;
2974                                 flags |= pd.enumeration ? 1 : 0;
2975                                 flags |= pd.integer_step ? 2 : 0;
2976                                 flags |= pd.logarithmic ? 4 : 0;
2977                                 flags |= pd.max_unbound ? 8 : 0;
2978                                 flags |= pd.min_unbound ? 16 : 0;
2979                                 flags |= pd.sr_dependent ? 32 : 0;
2980                                 flags |= pd.toggled ? 64 : 0;
2981                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
2982                                 lo_message_add_int32 (reply, flags);
2983
2984                                 lo_message_add_int32 (reply, pd.datatype);
2985                                 lo_message_add_float (reply, pd.lower);
2986                                 lo_message_add_float (reply, pd.upper);
2987                                 lo_message_add_string (reply, pd.print_fmt.c_str());
2988                                 if ( pd.scale_points ) {
2989                                         lo_message_add_int32 (reply, pd.scale_points->size());
2990                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
2991                                                 lo_message_add_int32 (reply, i->second);
2992                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
2993                                         }
2994                                 }
2995                                 else {
2996                                         lo_message_add_int32 (reply, 0);
2997                                 }
2998                                 if ( c ) {
2999                                         lo_message_add_double (reply, c->get_value());
3000                                 }
3001                                 else {
3002                                         lo_message_add_double (reply, 0);
3003                         }
3004                 }
3005         }
3006
3007         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3008         lo_message_free (reply);
3009
3010         return 0;
3011 }
3012
3013 int
3014 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3015         if (!session) {
3016                 return -1;
3017         }
3018
3019         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3020
3021         if (!r) {
3022                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3023                 return -1;
3024         }
3025
3026         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3027
3028         if (!redi) {
3029                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3030                 return -1;
3031         }
3032
3033         boost::shared_ptr<PluginInsert> pi;
3034
3035         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3036                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3037                 return -1;
3038         }
3039
3040         pi->reset_parameters_to_default ();
3041
3042         return 0;
3043 }
3044
3045 int
3046 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
3047 {
3048         if (!session)
3049                 return -1;
3050         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3051
3052         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3053
3054         if (!r) {
3055                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3056                 return -1;
3057         }
3058
3059         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3060
3061         if (!redi) {
3062                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3063                 return -1;
3064         }
3065
3066         boost::shared_ptr<PluginInsert> pi;
3067
3068         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3069                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3070                 return -1;
3071         }
3072
3073         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3074         bool ok=false;
3075
3076         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3077
3078         if (!ok) {
3079                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3080                 return -1;
3081         }
3082
3083         if (!pip->parameter_is_input(controlid)) {
3084                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3085                 return -1;
3086         }
3087
3088         ParameterDescriptor pd;
3089         pi->plugin()->get_parameter_descriptor (controlid,pd);
3090
3091         if (val >= pd.lower && val <= pd.upper) {
3092
3093                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3094                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3095                 c->set_value (val, PBD::Controllable::NoGroup);
3096         } else {
3097                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3098                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3099         }
3100
3101         return 0;
3102 }
3103
3104 //prints to cerr only
3105 int
3106 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3107 {
3108         if (!session) {
3109                 return -1;
3110         }
3111         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3112
3113         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3114
3115         if (!r) {
3116                 return -1;
3117         }
3118
3119         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3120
3121         if (!redi) {
3122                 return -1;
3123         }
3124
3125         boost::shared_ptr<PluginInsert> pi;
3126
3127         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3128                 return -1;
3129         }
3130
3131         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3132         bool ok=false;
3133
3134         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3135
3136         if (!ok) {
3137                 return -1;
3138         }
3139
3140         ParameterDescriptor pd;
3141
3142         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3143                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3144
3145                 cerr << "parameter:     " << pd.label  << "\n";
3146                 if (c) {
3147                         cerr << "current value: " << c->get_value () << "\n";
3148                 } else {
3149                         cerr << "current value not available, control does not exist\n";
3150                 }
3151                 cerr << "lower value:   " << pd.lower << "\n";
3152                 cerr << "upper value:   " << pd.upper << "\n";
3153         }
3154
3155         return 0;
3156 }
3157
3158 int
3159 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3160 {
3161         if (!session)
3162                 return -1;
3163         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3164
3165         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3166
3167         if (!r) {
3168                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3169                 return -1;
3170         }
3171
3172         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3173
3174         if (!redi) {
3175                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3176                 return -1;
3177         }
3178
3179         boost::shared_ptr<PluginInsert> pi;
3180
3181         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3182                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3183                 return -1;
3184         }
3185
3186         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3187         pi->activate();
3188
3189         return 0;
3190 }
3191
3192 int
3193 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3194 {
3195         if (!session)
3196                 return -1;
3197         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3198
3199         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3200
3201         if (!r) {
3202                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3203                 return -1;
3204         }
3205
3206         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3207
3208         if (!redi) {
3209                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3210                 return -1;
3211         }
3212
3213         boost::shared_ptr<PluginInsert> pi;
3214
3215         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3216                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3217                 return -1;
3218         }
3219
3220         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3221         pi->deactivate();
3222
3223         return 0;
3224 }
3225
3226 // select
3227
3228 int
3229 OSC::sel_pan_elevation (float val, lo_message msg)
3230 {
3231         OSCSurface *sur = get_surface(get_address (msg));
3232         boost::shared_ptr<Stripable> s;
3233         if (sur->expand_enable) {
3234                 s = get_strip (sur->expand, get_address (msg));
3235         } else {
3236                 s = _select;
3237         }
3238         if (s) {
3239                 if (s->pan_elevation_control()) {
3240                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3241                         return 0;
3242                 }
3243         }
3244         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3245 }
3246
3247 int
3248 OSC::sel_pan_frontback (float val, lo_message msg)
3249 {
3250         OSCSurface *sur = get_surface(get_address (msg));
3251         boost::shared_ptr<Stripable> s;
3252         if (sur->expand_enable) {
3253                 s = get_strip (sur->expand, get_address (msg));
3254         } else {
3255                 s = _select;
3256         }
3257         if (s) {
3258                 if (s->pan_frontback_control()) {
3259                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3260                         return 0;
3261                 }
3262         }
3263         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3264 }
3265
3266 int
3267 OSC::sel_pan_lfe (float val, lo_message msg)
3268 {
3269         OSCSurface *sur = get_surface(get_address (msg));
3270         boost::shared_ptr<Stripable> s;
3271         if (sur->expand_enable) {
3272                 s = get_strip (sur->expand, get_address (msg));
3273         } else {
3274                 s = _select;
3275         }
3276         if (s) {
3277                 if (s->pan_lfe_control()) {
3278                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3279                         return 0;
3280                 }
3281         }
3282         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3283 }
3284
3285 // compressor control
3286 int
3287 OSC::sel_comp_enable (float val, lo_message msg)
3288 {
3289         OSCSurface *sur = get_surface(get_address (msg));
3290         boost::shared_ptr<Stripable> s;
3291         if (sur->expand_enable) {
3292                 s = get_strip (sur->expand, get_address (msg));
3293         } else {
3294                 s = _select;
3295         }
3296         if (s) {
3297                 if (s->comp_enable_controllable()) {
3298                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3299                         return 0;
3300                 }
3301         }
3302         return sel_fail ("comp_enable", 0, get_address (msg));
3303 }
3304
3305 int
3306 OSC::sel_comp_threshold (float val, lo_message msg)
3307 {
3308         OSCSurface *sur = get_surface(get_address (msg));
3309         boost::shared_ptr<Stripable> s;
3310         if (sur->expand_enable) {
3311                 s = get_strip (sur->expand, get_address (msg));
3312         } else {
3313                 s = _select;
3314         }
3315         if (s) {
3316                 if (s->comp_threshold_controllable()) {
3317                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3318                         return 0;
3319                 }
3320         }
3321         return sel_fail ("comp_threshold", 0, get_address (msg));
3322 }
3323
3324 int
3325 OSC::sel_comp_speed (float val, lo_message msg)
3326 {
3327         OSCSurface *sur = get_surface(get_address (msg));
3328         boost::shared_ptr<Stripable> s;
3329         if (sur->expand_enable) {
3330                 s = get_strip (sur->expand, get_address (msg));
3331         } else {
3332                 s = _select;
3333         }
3334         if (s) {
3335                 if (s->comp_speed_controllable()) {
3336                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3337                         return 0;
3338                 }
3339         }
3340         return sel_fail ("comp_speed", 0, get_address (msg));
3341 }
3342
3343 int
3344 OSC::sel_comp_mode (float val, lo_message msg)
3345 {
3346         OSCSurface *sur = get_surface(get_address (msg));
3347         boost::shared_ptr<Stripable> s;
3348         if (sur->expand_enable) {
3349                 s = get_strip (sur->expand, get_address (msg));
3350         } else {
3351                 s = _select;
3352         }
3353         if (s) {
3354                 if (s->comp_mode_controllable()) {
3355                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3356                         return 0;
3357                 }
3358         }
3359         return sel_fail ("comp_mode", 0, get_address (msg));
3360 }
3361
3362 int
3363 OSC::sel_comp_makeup (float val, lo_message msg)
3364 {
3365         OSCSurface *sur = get_surface(get_address (msg));
3366         boost::shared_ptr<Stripable> s;
3367         if (sur->expand_enable) {
3368                 s = get_strip (sur->expand, get_address (msg));
3369         } else {
3370                 s = _select;
3371         }
3372         if (s) {
3373                 if (s->comp_makeup_controllable()) {
3374                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3375                         return 0;
3376                 }
3377         }
3378         return sel_fail ("comp_makeup", 0, get_address (msg));
3379 }
3380
3381 // EQ control
3382
3383 int
3384 OSC::sel_eq_enable (float val, lo_message msg)
3385 {
3386         OSCSurface *sur = get_surface(get_address (msg));
3387         boost::shared_ptr<Stripable> s;
3388         if (sur->expand_enable) {
3389                 s = get_strip (sur->expand, get_address (msg));
3390         } else {
3391                 s = _select;
3392         }
3393         if (s) {
3394                 if (s->eq_enable_controllable()) {
3395                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3396                         return 0;
3397                 }
3398         }
3399         return sel_fail ("eq_enable", 0, get_address (msg));
3400 }
3401
3402 int
3403 OSC::sel_eq_hpf (float val, lo_message msg)
3404 {
3405         OSCSurface *sur = get_surface(get_address (msg));
3406         boost::shared_ptr<Stripable> s;
3407         if (sur->expand_enable) {
3408                 s = get_strip (sur->expand, get_address (msg));
3409         } else {
3410                 s = _select;
3411         }
3412         if (s) {
3413                 if (s->eq_hpf_controllable()) {
3414                         s->eq_hpf_controllable()->set_value (s->eq_hpf_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3415                         return 0;
3416                 }
3417         }
3418         return sel_fail ("eq_hpf", 0, get_address (msg));
3419 }
3420
3421 int
3422 OSC::sel_eq_gain (int id, float val, lo_message msg)
3423 {
3424         OSCSurface *sur = get_surface(get_address (msg));
3425         boost::shared_ptr<Stripable> s;
3426         if (sur->expand_enable) {
3427                 s = get_strip (sur->expand, get_address (msg));
3428         } else {
3429                 s = _select;
3430         }
3431         if (s) {
3432                 if (id > 0) {
3433                         --id;
3434                 }
3435                 if (s->eq_gain_controllable (id)) {
3436                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3437                         return 0;
3438                 }
3439         }
3440         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3441 }
3442
3443 int
3444 OSC::sel_eq_freq (int id, float val, lo_message msg)
3445 {
3446         OSCSurface *sur = get_surface(get_address (msg));
3447         boost::shared_ptr<Stripable> s;
3448         if (sur->expand_enable) {
3449                 s = get_strip (sur->expand, get_address (msg));
3450         } else {
3451                 s = _select;
3452         }
3453         if (s) {
3454                 if (id > 0) {
3455                         --id;
3456                 }
3457                 if (s->eq_freq_controllable (id)) {
3458                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3459                         return 0;
3460                 }
3461         }
3462         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3463 }
3464
3465 int
3466 OSC::sel_eq_q (int id, float val, lo_message msg)
3467 {
3468         OSCSurface *sur = get_surface(get_address (msg));
3469         boost::shared_ptr<Stripable> s;
3470         if (sur->expand_enable) {
3471                 s = get_strip (sur->expand, get_address (msg));
3472         } else {
3473                 s = _select;
3474         }
3475         if (s) {
3476                 if (id > 0) {
3477                         --id;
3478                 }
3479                 if (s->eq_q_controllable (id)) {
3480                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3481                         return 0;
3482                 }
3483         }
3484         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3485 }
3486
3487 int
3488 OSC::sel_eq_shape (int id, float val, lo_message msg)
3489 {
3490         OSCSurface *sur = get_surface(get_address (msg));
3491         boost::shared_ptr<Stripable> s;
3492         if (sur->expand_enable) {
3493                 s = get_strip (sur->expand, get_address (msg));
3494         } else {
3495                 s = _select;
3496         }
3497         if (s) {
3498                 if (id > 0) {
3499                         --id;
3500                 }
3501                 if (s->eq_shape_controllable (id)) {
3502                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3503                         return 0;
3504                 }
3505         }
3506         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3507 }
3508
3509 void
3510 OSC::gui_selection_changed ()
3511 {
3512         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3513
3514         if (strip) {
3515                 _select = strip;
3516                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3517                         OSCSurface* sur = &_surface[it];
3518                         if(!sur->expand_enable) {
3519                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3520                                 _strip_select (strip, addr);
3521                         }
3522                 }
3523         }
3524 }
3525
3526 // timer callbacks
3527 bool
3528 OSC::periodic (void)
3529 {
3530         if (!tick) {
3531                 Glib::usleep(100); // let flurry of signals subside
3532                 if (global_init) {
3533                         for (uint32_t it = 0; it < _surface.size(); it++) {
3534                                 OSCSurface* sur = &_surface[it];
3535                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3536                                 global_feedback (sur->feedback, addr, sur->gainmode);
3537                         }
3538                         global_init = false;
3539                         tick = true;
3540                 }
3541                 if (bank_dirty) {
3542                         _recalcbanks ();
3543                         bank_dirty = false;
3544                         tick = true;
3545                 }
3546         }
3547
3548         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3549
3550                 OSCGlobalObserver* go;
3551
3552                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3553                         go->tick();
3554                 }
3555         }
3556         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3557
3558                 OSCRouteObserver* ro;
3559
3560                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3561                         ro->tick();
3562                 }
3563         }
3564         for (uint32_t it = 0; it < _surface.size(); it++) {
3565                 OSCSurface* sur = &_surface[it];
3566                 OSCSelectObserver* so;
3567                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3568                         so->tick();
3569                 }
3570         }
3571         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
3572
3573                 OSCCueObserver* co;
3574
3575                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
3576                         co->tick();
3577                 }
3578         }
3579         return true;
3580 }
3581
3582 int
3583 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3584 {
3585         OSCSurface *sur = get_surface(addr);
3586
3587         ostringstream os;
3588         lo_message reply;
3589         if (ssid) {
3590                 reply = lo_message_new ();
3591                 if (sur->feedback[2]) {
3592                         os << "/strip/" << path << "/" << ssid;
3593                 } else {
3594                         os << "/strip/" << path;
3595                         lo_message_add_int32 (reply, ssid);
3596                 }
3597                 string str_pth = os.str();
3598                 lo_message_add_float (reply, (float) val);
3599
3600                 lo_send_message (addr, str_pth.c_str(), reply);
3601                 lo_message_free (reply);
3602         }
3603         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3604                 os.str("");
3605                 os << "/select/" << path;
3606                 string sel_pth = os.str();
3607                 reply = lo_message_new ();
3608                 lo_message_add_float (reply, (float) val);
3609                 lo_send_message (addr, sel_pth.c_str(), reply);
3610                 lo_message_free (reply);
3611         }
3612
3613         return 0;
3614 }
3615
3616 int
3617 OSC::sel_fail (string path, float val, lo_address addr)
3618 {
3619         ostringstream os;
3620         os.str("");
3621         os << "/select/" << path;
3622         string sel_pth = os.str();
3623         lo_message reply = lo_message_new ();
3624         lo_message_add_float (reply, (float) val);
3625         lo_send_message (addr, sel_pth.c_str(), reply);
3626         lo_message_free (reply);
3627
3628         return 0;
3629 }
3630
3631 int
3632 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
3633 {
3634         OSCSurface *sur = get_surface(addr);
3635
3636         ostringstream os;
3637         lo_message reply;
3638         reply = lo_message_new ();
3639         if (sur->feedback[2]) {
3640                 os << "/select/" << path << "/" << id;
3641         } else {
3642                 os << "/select/" << path;
3643                 lo_message_add_int32 (reply, id);
3644         }
3645         string str_pth = os.str();
3646         lo_message_add_float (reply, (float) val);
3647
3648         lo_send_message (addr, str_pth.c_str(), reply);
3649         lo_message_free (reply);
3650
3651         return 0;
3652 }
3653
3654 XMLNode&
3655 OSC::get_state ()
3656 {
3657         XMLNode& node (ControlProtocol::get_state());
3658         node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
3659         node.add_property ("address-only", address_only);
3660         node.add_property ("remote-port", remote_port);
3661         node.add_property ("banksize", default_banksize);
3662         node.add_property ("striptypes", default_strip);
3663         node.add_property ("feedback", default_feedback);
3664         node.add_property ("gainmode", default_gainmode);
3665         if (_surface.size()) {
3666                 XMLNode* config = new XMLNode (X_("Configurations"));
3667                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3668                         OSCSurface* sur = &_surface[it];
3669                         XMLNode* devnode = new XMLNode (X_("Configuration"));
3670                         devnode->add_property (X_("url"), sur->remote_url);
3671                         devnode->add_property (X_("bank-size"), sur->bank_size);
3672                         devnode->add_property (X_("strip-types"), sur->strip_types.to_ulong());
3673                         devnode->add_property (X_("feedback"), sur->feedback.to_ulong());
3674                         devnode->add_property (X_("gainmode"), sur->gainmode);
3675                         config->add_child_nocopy (*devnode);
3676                 }
3677                 node.add_child_nocopy (*config);
3678         }
3679         return node;
3680 }
3681
3682 int
3683 OSC::set_state (const XMLNode& node, int version)
3684 {
3685         if (ControlProtocol::set_state (node, version)) {
3686                 return -1;
3687         }
3688         XMLProperty const * p = node.property (X_("debugmode"));
3689         if (p) {
3690                 _debugmode = OSCDebugMode (PBD::atoi(p->value ()));
3691         }
3692         p = node.property (X_("address-only"));
3693         if (p) {
3694                 address_only = OSCDebugMode (PBD::atoi(p->value ()));
3695         }
3696         p = node.property (X_("remote-port"));
3697         if (p) {
3698                 remote_port = p->value ();
3699         }
3700         p = node.property (X_("banksize"));
3701         if (p) {
3702                 default_banksize = OSCDebugMode (PBD::atoi(p->value ()));
3703         }
3704         p = node.property (X_("striptypes"));
3705         if (p) {
3706                 default_strip = OSCDebugMode (PBD::atoi(p->value ()));
3707         }
3708         p = node.property (X_("feedback"));
3709         if (p) {
3710                 default_feedback = OSCDebugMode (PBD::atoi(p->value ()));
3711         }
3712         p = node.property (X_("gainmode"));
3713         if (p) {
3714                 default_gainmode = OSCDebugMode (PBD::atoi(p->value ()));
3715         }
3716         XMLNode* cnode = node.child (X_("Configurations"));
3717
3718         if (cnode) {
3719                 XMLNodeList const& devices = cnode->children();
3720                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
3721                         XMLProperty const * prop = (*d)->property (X_("url"));
3722                         if (prop) {
3723                                 OSCSurface s;
3724                                 bank_dirty = true;
3725                                 s.remote_url = prop->value();
3726                                 prop = (*d)->property (X_("bank-size"));
3727                                 if (prop) {
3728                                         s.bank_size = atoi (prop->value().c_str());
3729                                 }
3730                                 prop = (*d)->property (X_("strip-types"));
3731                                 if (prop) {
3732                                         s.strip_types = atoi (prop->value().c_str());
3733                                 }
3734                                 prop = (*d)->property (X_("feedback"));
3735                                 if (prop) {
3736                                         s.feedback = atoi (prop->value().c_str());
3737                                 }
3738                                 prop = (*d)->property (X_("gainmode"));
3739                                 if (prop) {
3740                                         s.gainmode = atoi (prop->value().c_str());
3741                                 }
3742                                 s.bank = 1;
3743                                 s.sel_obs = 0;
3744                                 s.expand = 0;
3745                                 s.expand_enable = false;
3746                                 s.strips = get_sorted_stripables(s.strip_types, s.cue);
3747                                 s.nstrips = s.strips.size();
3748                                 _surface.push_back (s);
3749                         }
3750                 }
3751         }
3752         global_init = true;
3753         tick = false;
3754
3755         return 0;
3756 }
3757
3758 // predicate for sort call in get_sorted_stripables
3759 struct StripableByPresentationOrder
3760 {
3761         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
3762         {
3763                 return a->presentation_info().order() < b->presentation_info().order();
3764         }
3765
3766         bool operator () (const Stripable & a, const Stripable & b) const
3767         {
3768                 return a.presentation_info().order() < b.presentation_info().order();
3769         }
3770
3771         bool operator () (const Stripable * a, const Stripable * b) const
3772         {
3773                 return a->presentation_info().order() < b->presentation_info().order();
3774         }
3775 };
3776
3777 OSC::Sorted
3778 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
3779 {
3780         Sorted sorted;
3781
3782         // fetch all stripables
3783         StripableList stripables;
3784
3785         session->get_stripables (stripables);
3786
3787         // Look for stripables that match bit in sur->strip_types
3788         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
3789
3790                 boost::shared_ptr<Stripable> s = *it;
3791                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3792                         // do nothing... skip it
3793                 } else {
3794
3795                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
3796                                 sorted.push_back (s);
3797                         } else
3798                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
3799                                 sorted.push_back (s);
3800                         } else
3801                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
3802                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3803                                 // r->feeds (session->master_out()) may make more sense
3804                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
3805                                         // this is a bus
3806                                         if (types[2]) {
3807                                                 sorted.push_back (s);
3808                                         }
3809                                 } else {
3810                                         // this is an Aux out
3811                                         if (types[7]) {
3812                                                 sorted.push_back (s);
3813                                         }
3814                                 }
3815                         } else
3816                         if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
3817                                 sorted.push_back (s);
3818                         } else
3819                         if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
3820                                 sorted.push_back (s);
3821                         } else
3822                         if (types[8] && (s->presentation_info().flags() & PresentationInfo::Selected)) {
3823                                 sorted.push_back (s);
3824                         } else
3825                         if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3826                                 sorted.push_back (s);
3827                         }
3828                 }
3829         }
3830         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
3831         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
3832         if (types[5]) {
3833                 sorted.push_back (session->master_out());
3834         }
3835         if (types[6]) {
3836                 sorted.push_back (session->monitor_out());
3837         }
3838         return sorted;
3839 }
3840
3841 int
3842 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
3843 {
3844         int ret = 1; /* unhandled */
3845
3846         if (!strncmp (path, "/cue/aux", 8)) {
3847                 // set our Aux bus
3848                 cue_set (argv[0]->i, msg);
3849                 ret = 0;
3850         }
3851         else if (!strncmp (path, "/cue/connect", 12)) {
3852                 // switch to next Aux bus
3853                 cue_set (0, msg);
3854                 ret = 0;
3855         }
3856         else if (!strncmp (path, "/cue/next_aux", 13)) {
3857                 // switch to next Aux bus
3858                 cue_next (msg);
3859                 ret = 0;
3860         }
3861         else if (!strncmp (path, "/cue/previous_aux", 17)) {
3862                 // switch to previous Aux bus
3863                 cue_previous (msg);
3864                 ret = 0;
3865         }
3866         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
3867                 int id = atoi (&path[16]);
3868                 cue_send_fader (id, argv[0]->f, msg);
3869                 ret = 0;
3870         }
3871         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
3872                 int id = atoi (&path[17]);
3873                 cue_send_enable (id, argv[0]->f, msg);
3874                 ret = 0;
3875         }
3876         else if (!strncmp (path, "/cue/fader", 10)) {
3877                 cue_aux_fader (argv[0]->f, msg);
3878                 ret = 0;
3879         }
3880         else if (!strncmp (path, "/cue/mute", 9)) {
3881                 cue_aux_mute (argv[0]->f, msg);
3882                 ret = 0;
3883         }
3884
3885         if ((ret && _debugmode == Unhandled)) {
3886                 debugmsg (_("Unhandled OSC cue message"), path, types, argv, argc);
3887         } else if ((!ret && _debugmode == All)) {
3888                 debugmsg (_("OSC cue"), path, types, argv, argc);
3889         }
3890
3891         return ret;
3892 }
3893
3894 int
3895 OSC::cue_set (uint32_t aux, lo_message msg)
3896 {
3897         return _cue_set (aux, get_address (msg));
3898 }
3899
3900 int
3901 OSC::_cue_set (uint32_t aux, lo_address addr)
3902 {
3903         OSCSurface *s = get_surface(addr);
3904         s->bank_size = 0;
3905         s->strip_types = 128;
3906         s->feedback = 0;
3907         s->gainmode = 1;
3908         s->cue = true;
3909         s->aux = aux;
3910         s->strips = get_sorted_stripables(s->strip_types, s->cue);
3911
3912         s->nstrips = s->strips.size();
3913         // get rid of any old CueObsevers for this address
3914         cueobserver_connections.drop_connections ();
3915         CueObservers::iterator x;
3916         for (x = cue_observers.begin(); x != cue_observers.end();) {
3917
3918                 OSCCueObserver* co;
3919
3920                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
3921
3922                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
3923
3924                         if (res == 0) {
3925                                 delete *x;
3926                                 x = cue_observers.erase (x);
3927                         } else {
3928                                 ++x;
3929                         }
3930                 } else {
3931                         ++x;
3932                 }
3933         }
3934
3935         // get a list of Auxes
3936         for (uint32_t n = 0; n < s->nstrips; ++n) {
3937                 boost::shared_ptr<Stripable> stp = s->strips[n];
3938                 if (stp) {
3939                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
3940                         if (aux == n+1) {
3941                                 // aux must be at least one
3942                                 // need a signal if aux vanishes
3943                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
3944
3945                                 // make a list of stripables with sends that go to this bus
3946                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
3947                                 // start cue observer
3948                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
3949                                 cue_observers.push_back (co);
3950                         }
3951
3952                 }
3953         }
3954
3955         return 0;
3956 }
3957
3958 int
3959 OSC::cue_next (lo_message msg)
3960 {
3961         OSCSurface *s = get_surface(get_address (msg));
3962         if (s->aux < s->nstrips) {
3963                 cue_set (s->aux + 1, msg);
3964         } else {
3965                 cue_set (s->nstrips, msg);
3966         }
3967         return 0;
3968 }
3969
3970 int
3971 OSC::cue_previous (lo_message msg)
3972 {
3973         OSCSurface *s = get_surface(get_address (msg));
3974         if (s->aux > 1) {
3975                 cue_set (s->aux - 1, msg);
3976         }
3977         return 0;
3978 }
3979
3980 boost::shared_ptr<Send>
3981 OSC::cue_get_send (uint32_t id, lo_address addr)
3982 {
3983         OSCSurface *s = get_surface(addr);
3984         if (id && s->aux > 0 && id <= s->sends.size()) {
3985                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
3986                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
3987                 if (r && aux) {
3988                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
3989                 }
3990         }
3991         return boost::shared_ptr<Send>();
3992
3993 }
3994
3995 int
3996 OSC::cue_aux_fader (float position, lo_message msg)
3997 {
3998         if (!session) return -1;
3999
4000         OSCSurface *sur = get_surface(get_address (msg));
4001         if (sur->cue) {
4002                 if (sur->aux) {
4003                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4004
4005                         if (s) {
4006                                 float abs;
4007                                 abs = slider_position_to_gain_with_max (position, 2.0);
4008                                 if (s->gain_control()) {
4009                                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4010                                         return 0;
4011                                 }
4012                         }
4013                 }
4014         }
4015         return cue_float_message ("/cue/fader", 0, get_address (msg));
4016 }
4017
4018 int
4019 OSC::cue_aux_mute (float state, lo_message msg)
4020 {
4021         if (!session) return -1;
4022
4023         OSCSurface *sur = get_surface(get_address (msg));
4024         if (sur->cue) {
4025                 if (sur->aux) {
4026                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4027                         if (s) {
4028                                 if (s->mute_control()) {
4029                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
4030                                         return 0;
4031                                 }
4032                         }
4033                 }
4034         }
4035         return cue_float_message ("/cue/mute", 0, get_address (msg));
4036 }
4037
4038 int
4039 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
4040 {
4041         if (!session) {
4042                 return -1;
4043         }
4044         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4045         float abs;
4046         if (s) {
4047                 if (s->gain_control()) {
4048                         abs = slider_position_to_gain_with_max (val, 2.0);
4049                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4050                         return 0;
4051                 }
4052         }
4053         return cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
4054 }
4055
4056 int
4057 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
4058 {
4059         if (!session)
4060                 return -1;
4061         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4062         if (s) {
4063                 if (state) {
4064                         s->activate ();
4065                 } else {
4066                         s->deactivate ();
4067                 }
4068                 return 0;
4069         }
4070         return cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
4071 }
4072
4073 int
4074 OSC::cue_float_message (string path, float val, lo_address addr)
4075 {
4076
4077         lo_message reply;
4078         reply = lo_message_new ();
4079         lo_message_add_float (reply, (float) val);
4080
4081         lo_send_message (addr, path.c_str(), reply);
4082         lo_message_free (reply);
4083
4084         return 0;
4085 }
4086
4087 int
4088 OSC::text_message (string path, string val, lo_address addr)
4089 {
4090
4091         lo_message reply;
4092         reply = lo_message_new ();
4093         lo_message_add_string (reply, val.c_str());
4094
4095         lo_send_message (addr, path.c_str(), reply);
4096         lo_message_free (reply);
4097
4098         return 0;
4099 }
4100
4101
4102 // we have to have a sorted list of stripables that have sends pointed at our aux
4103 // we can use the one in osc.cc to get an aux list
4104 OSC::Sorted
4105 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
4106 {
4107         Sorted sorted;
4108         cueobserver_connections.drop_connections ();
4109         // fetch all stripables
4110         StripableList stripables;
4111
4112         session->get_stripables (stripables);
4113
4114         // Look for stripables that have a send to aux
4115         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4116
4117                 boost::shared_ptr<Stripable> s = *it;
4118                 // we only want routes
4119                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4120                 if (r) {
4121                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
4122                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4123                         if (snd) { // test for send to aux
4124                                 sorted.push_back (s);
4125                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
4126                         }
4127                 }
4128
4129
4130         }
4131         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4132
4133         return sorted;
4134 }
4135