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