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