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