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