OSC: Slow down data storm at surface refresh
[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 == All)) {
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                 // slow devices need time to clear buffers
1247                 usleep ((uint32_t) 10);
1248         }
1249         // Should maybe do global_observers too
1250         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
1251
1252                 OSCGlobalObserver* gc;
1253
1254                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1255                         delete *x;
1256                         x = global_observers.erase (x);
1257                 } else {
1258                         ++x;
1259                 }
1260         }
1261         // delete select observers
1262         for (uint32_t it = 0; it < _surface.size(); ++it) {
1263                 OSCSurface* sur = &_surface[it];
1264                 OSCSelectObserver* so;
1265                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
1266                         delete so;
1267                 }
1268         }
1269         // clear out surfaces
1270         _surface.clear();
1271 }
1272
1273 int
1274 OSC::set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gm, lo_message msg)
1275 {
1276         OSCSurface *s = get_surface(get_address (msg));
1277         s->bank_size = b_size;
1278         s->strip_types = strips;
1279         s->feedback = fb;
1280         s->gainmode = gm;
1281         // set bank and strip feedback
1282         set_bank(s->bank, msg);
1283
1284         global_feedback (s->feedback, get_address (msg), s->gainmode);
1285         return 0;
1286 }
1287
1288 int
1289 OSC::set_surface_bank_size (uint32_t bs, lo_message msg)
1290 {
1291         OSCSurface *s = get_surface(get_address (msg));
1292         s->bank_size = bs;
1293
1294         // set bank and strip feedback
1295         set_bank(s->bank, msg);
1296         return 0;
1297 }
1298
1299 int
1300 OSC::set_surface_strip_types (uint32_t st, lo_message msg)
1301 {
1302         OSCSurface *s = get_surface(get_address (msg));
1303         s->strip_types = st;
1304
1305         // set bank and strip feedback
1306         set_bank(s->bank, msg);
1307         return 0;
1308 }
1309
1310
1311 int
1312 OSC::set_surface_feedback (uint32_t fb, lo_message msg)
1313 {
1314         OSCSurface *s = get_surface(get_address (msg));
1315         s->feedback = fb;
1316
1317         // set bank and strip feedback
1318         set_bank(s->bank, msg);
1319
1320         // Set global/master feedback
1321         global_feedback (s->feedback, get_address (msg), s->gainmode);
1322         return 0;
1323 }
1324
1325
1326 int
1327 OSC::set_surface_gainmode (uint32_t gm, lo_message msg)
1328 {
1329         OSCSurface *s = get_surface(get_address (msg));
1330         s->gainmode = gm;
1331
1332         // set bank and strip feedback
1333         set_bank(s->bank, msg);
1334
1335         // Set global/master feedback
1336         global_feedback (s->feedback, get_address (msg), s->gainmode);
1337         return 0;
1338 }
1339
1340 OSC::OSCSurface *
1341 OSC::get_surface (lo_address addr)
1342 {
1343         string r_url;
1344         char * rurl;
1345         rurl = lo_address_get_url (addr);
1346         r_url = rurl;
1347         free (rurl);
1348         for (uint32_t it = 0; it < _surface.size(); ++it) {
1349                 //find setup for this server
1350                 if (!_surface[it].remote_url.find(r_url)){
1351                         return &_surface[it];
1352                 }
1353         }
1354         // if we do this when OSC is started we get the wrong stripable
1355         // we don't need this until we actually have a surface to deal with
1356         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1357                 gui_selection_changed();
1358         }
1359
1360         // No surface create one with default values
1361         OSCSurface s;
1362         s.remote_url = r_url;
1363         s.bank = 1;
1364         s.bank_size = default_banksize; // need to find out how many strips there are
1365         s.strip_types = default_strip; // 31 is tracks, busses, and VCAs (no master/monitor)
1366         s.feedback = default_feedback;
1367         s.gainmode = default_gainmode;
1368         s.sel_obs = 0;
1369         s.expand = 0;
1370         s.expand_enable = false;
1371         s.strips = get_sorted_stripables(s.strip_types);
1372
1373         s.nstrips = s.strips.size();
1374         _surface.push_back (s);
1375
1376         return &_surface[_surface.size() - 1];
1377 }
1378
1379 // setup global feedback for a surface
1380 void
1381 OSC::global_feedback (bitset<32> feedback, lo_address addr, uint32_t gainmode)
1382 {
1383         // first destroy global observer for this surface
1384         GlobalObservers::iterator x;
1385         for (x = global_observers.begin(); x != global_observers.end();) {
1386
1387                 OSCGlobalObserver* ro;
1388
1389                 if ((ro = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1390
1391                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
1392
1393                         if (res == 0) {
1394                                 delete *x;
1395                                 x = global_observers.erase (x);
1396                         } else {
1397                                 ++x;
1398                         }
1399                 } else {
1400                         ++x;
1401                 }
1402         }
1403         if (feedback[4] || feedback[3] || feedback[5] || feedback[6]) {
1404                 // create a new Global Observer for this surface
1405                 OSCGlobalObserver* o = new OSCGlobalObserver (*session, addr, gainmode, /*s->*/feedback);
1406                 global_observers.push_back (o);
1407         }
1408 }
1409
1410 void
1411 OSC::notify_routes_added (ARDOUR::RouteList &)
1412 {
1413         // not sure if we need this PI change seems to cover
1414         //recalcbanks();
1415 }
1416
1417 void
1418 OSC::notify_vca_added (ARDOUR::VCAList &)
1419 {
1420         // not sure if we need this PI change seems to cover
1421         //recalcbanks();
1422 }
1423
1424 void
1425 OSC::recalcbanks ()
1426 {
1427         tick = false;
1428         bank_dirty = true;
1429 }
1430
1431 void
1432 OSC::_recalcbanks ()
1433 {
1434         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1435                 _select = ControlProtocol::first_selected_stripable();
1436         }
1437
1438         // do a set_bank for each surface we know about.
1439         for (uint32_t it = 0; it < _surface.size(); ++it) {
1440                 OSCSurface* sur = &_surface[it];
1441                 // find lo_address
1442                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
1443                 _set_bank (sur->bank, addr);
1444         }
1445 }
1446
1447 /*
1448  * This gets called not only when bank changes but also:
1449  *  - bank size change
1450  *  - feedback change
1451  *  - strip types changes
1452  *  - fadermode changes
1453  *  - stripable creation/deletion/flag
1454  *  - to refresh what is "displayed"
1455  * Basically any time the bank needs to be rebuilt
1456  */
1457 int
1458 OSC::set_bank (uint32_t bank_start, lo_message msg)
1459 {
1460         return _set_bank (bank_start, get_address (msg));
1461 }
1462
1463 // set bank is callable with either message or address
1464 int
1465 OSC::_set_bank (uint32_t bank_start, lo_address addr)
1466 {
1467         if (!session) {
1468                 return -1;
1469         }
1470         // no nstripables yet
1471         if (!session->nroutes()) {
1472                 return -1;
1473         }
1474
1475         OSCSurface *s = get_surface (addr);
1476
1477         // revert any expand to select
1478          s->expand = 0;
1479          s->expand_enable = false;
1480         _strip_select (ControlProtocol::first_selected_stripable(), addr);
1481
1482         // undo all listeners for this url
1483         StripableList stripables;
1484         session->get_stripables (stripables);
1485         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
1486
1487                 boost::shared_ptr<Stripable> stp = *it;
1488                 if (stp) {
1489                         end_listen (stp, addr);
1490                 }
1491                 // slow devices need time to clear buffers
1492                 usleep ((uint32_t) 10);
1493         }
1494
1495         s->strips = get_sorted_stripables(s->strip_types);
1496         s->nstrips = s->strips.size();
1497
1498         uint32_t b_size;
1499         if (!s->bank_size) {
1500                 // no banking - bank includes all stripables
1501                 b_size = s->nstrips;
1502         } else {
1503                 b_size = s->bank_size;
1504         }
1505
1506         // Do limits checking
1507         if (bank_start < 1) bank_start = 1;
1508         if (b_size >= s->nstrips)  {
1509                 bank_start = 1;
1510         } else if (bank_start > ((s->nstrips - b_size) + 1)) {
1511                 // top bank is always filled if there are enough strips for at least one bank
1512                 bank_start = (uint32_t)((s->nstrips - b_size) + 1);
1513         }
1514         //save bank in case we have had to change it
1515         s->bank = bank_start;
1516
1517         if (s->feedback[0] || s->feedback[1]) {
1518
1519                 for (uint32_t n = bank_start; n < (min ((b_size + bank_start), s->nstrips + 1)); ++n) {
1520                         if (n <= s->strips.size()) {
1521                                 boost::shared_ptr<Stripable> stp = s->strips[n - 1];
1522
1523                                 if (stp) {
1524                                         listen_to_route(stp, addr);
1525                                 }
1526                         }
1527                         // slow devices need time to clear buffers
1528                         usleep ((uint32_t) 10);
1529                 }
1530         }
1531         // light bankup or bankdown buttons if it is possible to bank in that direction
1532         if (s->feedback[4]) {
1533                 // these two messages could be bundled
1534                 lo_message reply;
1535                 reply = lo_message_new ();
1536                 if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
1537                         lo_message_add_int32 (reply, 0);
1538                 } else {
1539                         lo_message_add_int32 (reply, 1);
1540                 }
1541                 lo_send_message (addr, "/bank_up", reply);
1542                 lo_message_free (reply);
1543                 reply = lo_message_new ();
1544                 if (s->bank > 1) {
1545                         lo_message_add_int32 (reply, 1);
1546                 } else {
1547                         lo_message_add_int32 (reply, 0);
1548                 }
1549                 lo_send_message (addr, "/bank_down", reply);
1550                 lo_message_free (reply);
1551         }
1552         bank_dirty = false;
1553         tick = true;
1554         return 0;
1555 }
1556
1557 int
1558 OSC::bank_up (lo_message msg)
1559 {
1560         if (!session) {
1561                 return -1;
1562         }
1563         OSCSurface *s = get_surface(get_address (msg));
1564         set_bank (s->bank + s->bank_size, msg);
1565         return 0;
1566 }
1567
1568 int
1569 OSC::bank_down (lo_message msg)
1570 {
1571         if (!session) {
1572                 return -1;
1573         }
1574         OSCSurface *s = get_surface(get_address (msg));
1575         if (s->bank < s->bank_size) {
1576                 set_bank (1, msg);
1577         } else {
1578                 set_bank (s->bank - s->bank_size, msg);
1579         }
1580         return 0;
1581 }
1582
1583 uint32_t
1584 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
1585 {
1586         if (!strip) {
1587                 return 0;
1588         }
1589
1590         OSCSurface *s = get_surface(addr);
1591
1592         uint32_t b_size;
1593         if (!s->bank_size) {
1594                 // no banking
1595                 b_size = s->nstrips;
1596         } else {
1597                 b_size = s->bank_size;
1598         }
1599
1600         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
1601                 if (n <= s->strips.size()) {
1602                         if (strip == s->strips[n-1]) {
1603                                 return n - s->bank + 1;
1604                         }
1605                 }
1606         }
1607         // failsafe... should never get here.
1608         return 0;
1609 }
1610
1611 boost::shared_ptr<ARDOUR::Stripable>
1612 OSC::get_strip (uint32_t ssid, lo_address addr)
1613 {
1614         OSCSurface *s = get_surface(addr);
1615         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
1616                 return s->strips[ssid + s->bank - 2];
1617         }
1618         // guess it is out of range
1619         return boost::shared_ptr<ARDOUR::Stripable>();
1620 }
1621
1622 void
1623 OSC::transport_frame (lo_message msg)
1624 {
1625         if (!session) {
1626                 return;
1627         }
1628         framepos_t pos = session->transport_frame ();
1629
1630         lo_message reply = lo_message_new ();
1631         lo_message_add_int64 (reply, pos);
1632
1633         lo_send_message (get_address (msg), "/transport_frame", reply);
1634
1635         lo_message_free (reply);
1636 }
1637
1638 void
1639 OSC::transport_speed (lo_message msg)
1640 {
1641         if (!session) {
1642                 return;
1643         }
1644         double ts = session->transport_speed ();
1645
1646         lo_message reply = lo_message_new ();
1647         lo_message_add_double (reply, ts);
1648
1649         lo_send_message (get_address (msg), "/transport_speed", reply);
1650
1651         lo_message_free (reply);
1652 }
1653
1654 void
1655 OSC::record_enabled (lo_message msg)
1656 {
1657         if (!session) {
1658                 return;
1659         }
1660         int re = (int)session->get_record_enabled ();
1661
1662         lo_message reply = lo_message_new ();
1663         lo_message_add_int32 (reply, re);
1664
1665         lo_send_message (get_address (msg), "/record_enabled", reply);
1666
1667         lo_message_free (reply);
1668 }
1669
1670 // master and monitor calls
1671 int
1672 OSC::master_set_gain (float dB)
1673 {
1674         if (!session) return -1;
1675         boost::shared_ptr<Stripable> s = session->master_out();
1676         if (s) {
1677                 if (dB < -192) {
1678                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1679                 } else {
1680                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1681                 }
1682         }
1683         return 0;
1684 }
1685
1686 int
1687 OSC::master_set_fader (float position)
1688 {
1689         if (!session) return -1;
1690         boost::shared_ptr<Stripable> s = session->master_out();
1691         if (s) {
1692                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1693         }
1694         return 0;
1695 }
1696
1697 int
1698 OSC::master_set_trim (float dB)
1699 {
1700         if (!session) return -1;
1701         boost::shared_ptr<Stripable> s = session->master_out();
1702
1703         if (s) {
1704                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1705         }
1706
1707         return 0;
1708 }
1709
1710 int
1711 OSC::master_set_pan_stereo_position (float position, lo_message msg)
1712 {
1713         if (!session) return -1;
1714
1715         float endposition = .5;
1716         boost::shared_ptr<Stripable> s = session->master_out();
1717
1718         if (s) {
1719                 if (s->pan_azimuth_control()) {
1720                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
1721                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
1722                 }
1723         }
1724         OSCSurface *sur = get_surface(get_address (msg));
1725
1726         if (sur->feedback[4]) {
1727                 lo_message reply = lo_message_new ();
1728                 lo_message_add_float (reply, endposition);
1729
1730                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
1731                 lo_message_free (reply);
1732         }
1733
1734         return 0;
1735 }
1736
1737 int
1738 OSC::master_set_mute (uint32_t state)
1739 {
1740         if (!session) return -1;
1741
1742         boost::shared_ptr<Stripable> s = session->master_out();
1743
1744         if (s) {
1745                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
1746         }
1747
1748         return 0;
1749 }
1750
1751 int
1752 OSC::monitor_set_gain (float dB)
1753 {
1754         if (!session) return -1;
1755         boost::shared_ptr<Stripable> s = session->monitor_out();
1756
1757         if (s) {
1758                 if (dB < -192) {
1759                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1760                 } else {
1761                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1762                 }
1763         }
1764         return 0;
1765 }
1766
1767 int
1768 OSC::monitor_set_fader (float position)
1769 {
1770         if (!session) return -1;
1771         boost::shared_ptr<Stripable> s = session->monitor_out();
1772         if (s) {
1773                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1774         }
1775         return 0;
1776 }
1777
1778 // strip calls
1779 int
1780 OSC::route_mute (int ssid, int yn, lo_message msg)
1781 {
1782         if (!session) return -1;
1783         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1784
1785         if (s) {
1786                 if (s->mute_control()) {
1787                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1788                         return 0;
1789                 }
1790         }
1791
1792         return route_send_fail ("mute", ssid, 0, get_address (msg));
1793 }
1794
1795 int
1796 OSC::sel_mute (uint32_t yn, lo_message msg)
1797 {
1798         OSCSurface *sur = get_surface(get_address (msg));
1799         boost::shared_ptr<Stripable> s;
1800         if (sur->expand_enable) {
1801                 s = get_strip (sur->expand, get_address (msg));
1802         } else {
1803                 s = _select;
1804         }
1805         if (s) {
1806                 if (s->mute_control()) {
1807                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1808                         return 0;
1809                 }
1810         }
1811         return sel_fail ("mute", 0, get_address (msg));
1812 }
1813
1814 int
1815 OSC::route_solo (int ssid, int yn, lo_message msg)
1816 {
1817         if (!session) return -1;
1818         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1819
1820         if (s) {
1821                 if (s->solo_control()) {
1822                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1823                         return 0;
1824                 }
1825         }
1826
1827         return route_send_fail ("solo", ssid, 0, get_address (msg));
1828 }
1829
1830 int
1831 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
1832 {
1833         if (!session) return -1;
1834         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1835
1836         if (s) {
1837                 if (s->solo_isolate_control()) {
1838                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1839                         return 0;
1840                 }
1841         }
1842
1843         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
1844 }
1845
1846 int
1847 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
1848 {
1849         if (!session) return -1;
1850         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
1851
1852         if (s) {
1853                 if (s->solo_safe_control()) {
1854                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1855                         return 0;
1856                 }
1857         }
1858
1859         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
1860 }
1861
1862 int
1863 OSC::sel_solo (uint32_t yn, lo_message msg)
1864 {
1865         OSCSurface *sur = get_surface(get_address (msg));
1866         boost::shared_ptr<Stripable> s;
1867         if (sur->expand_enable) {
1868                 s = get_strip (sur->expand, get_address (msg));
1869         } else {
1870                 s = _select;
1871         }
1872         if (s) {
1873                 if (s->solo_control()) {
1874                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1875                         return 0;
1876                 }
1877         }
1878         return sel_fail ("solo", 0, get_address (msg));
1879 }
1880
1881 int
1882 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
1883 {
1884         OSCSurface *sur = get_surface(get_address (msg));
1885         boost::shared_ptr<Stripable> s;
1886         if (sur->expand_enable) {
1887                 s = get_strip (sur->expand, get_address (msg));
1888         } else {
1889                 s = _select;
1890         }
1891         if (s) {
1892                 if (s->solo_isolate_control()) {
1893                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1894                         return 0;
1895                 }
1896         }
1897         return sel_fail ("solo_iso", 0, get_address (msg));
1898 }
1899
1900 int
1901 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
1902 {
1903         OSCSurface *sur = get_surface(get_address (msg));
1904         boost::shared_ptr<Stripable> s;
1905         if (sur->expand_enable) {
1906                 s = get_strip (sur->expand, get_address (msg));
1907         } else {
1908                 s = _select;
1909         }
1910         if (s) {
1911                 if (s->solo_safe_control()) {
1912                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1913                         return 0;
1914                 }
1915         }
1916         return sel_fail ("solo_safe", 0, get_address (msg));
1917 }
1918
1919 int
1920 OSC::sel_recenable (uint32_t yn, lo_message msg)
1921 {
1922         OSCSurface *sur = get_surface(get_address (msg));
1923         boost::shared_ptr<Stripable> s;
1924         if (sur->expand_enable) {
1925                 s = get_strip (sur->expand, get_address (msg));
1926         } else {
1927                 s = _select;
1928         }
1929         if (s) {
1930                 if (s->rec_enable_control()) {
1931                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1932                         return 0;
1933                 }
1934         }
1935         return sel_fail ("recenable", 0, get_address (msg));
1936 }
1937
1938 int
1939 OSC::route_recenable (int ssid, int yn, lo_message msg)
1940 {
1941         if (!session) return -1;
1942         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1943
1944         if (s) {
1945                 if (s->rec_enable_control()) {
1946                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
1947                         if (s->rec_enable_control()->get_value()) {
1948                                 return 0;
1949                         }
1950                 }
1951         }
1952         return route_send_fail ("recenable", ssid, 0, get_address (msg));
1953 }
1954
1955 int
1956 OSC::sel_recsafe (uint32_t yn, lo_message msg)
1957 {
1958         OSCSurface *sur = get_surface(get_address (msg));
1959         boost::shared_ptr<Stripable> s;
1960         if (sur->expand_enable) {
1961                 s = get_strip (sur->expand, get_address (msg));
1962         } else {
1963                 s = _select;
1964         }
1965         if (s) {
1966                 if (s->rec_safe_control()) {
1967                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
1968                         return 0;
1969                 }
1970         }
1971         return sel_fail ("record_safe", 0, get_address (msg));
1972 }
1973
1974 int
1975 OSC::route_recsafe (int ssid, int yn, lo_message msg)
1976 {
1977         if (!session) return -1;
1978         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1979         if (s) {
1980                 if (s->rec_safe_control()) {
1981                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
1982                         if (s->rec_safe_control()->get_value()) {
1983                                 return 0;
1984                         }
1985                 }
1986         }
1987         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
1988 }
1989
1990 int
1991 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
1992 {
1993         if (!session) return -1;
1994         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
1995
1996         if (s) {
1997                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
1998                 if (track) {
1999                         if (track->monitoring_control()) {
2000                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2001                                 return 0;
2002                         }
2003                 }
2004         }
2005
2006         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2007 }
2008
2009 int
2010 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2011 {
2012         OSCSurface *sur = get_surface(get_address (msg));
2013         boost::shared_ptr<Stripable> s;
2014         if (sur->expand_enable) {
2015                 s = get_strip (sur->expand, get_address (msg));
2016         } else {
2017                 s = _select;
2018         }
2019         if (s) {
2020                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2021                 if (track) {
2022                         if (track->monitoring_control()) {
2023                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2024                                 return 0;
2025                         }
2026                 }
2027         }
2028         return sel_fail ("monitor_input", 0, get_address (msg));
2029 }
2030
2031 int
2032 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2033 {
2034         if (!session) return -1;
2035         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2036
2037         if (s) {
2038                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2039                 if (track) {
2040                         if (track->monitoring_control()) {
2041                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2042                                 return 0;
2043                         }
2044                 }
2045         }
2046
2047         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2048 }
2049
2050 int
2051 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2052 {
2053         OSCSurface *sur = get_surface(get_address (msg));
2054         boost::shared_ptr<Stripable> s;
2055         if (sur->expand_enable) {
2056                 s = get_strip (sur->expand, get_address (msg));
2057         } else {
2058                 s = _select;
2059         }
2060         if (s) {
2061                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2062                 if (track) {
2063                         if (track->monitoring_control()) {
2064                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2065                                 return 0;
2066                         }
2067                 }
2068         }
2069         return sel_fail ("monitor_disk", 0, get_address (msg));
2070 }
2071
2072
2073 int
2074 OSC::strip_phase (int ssid, int yn, lo_message msg)
2075 {
2076         if (!session) return -1;
2077         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2078
2079         if (s) {
2080                 if (s->phase_control()) {
2081                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2082                         return 0;
2083                 }
2084         }
2085
2086         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2087 }
2088
2089 int
2090 OSC::sel_phase (uint32_t yn, lo_message msg)
2091 {
2092         OSCSurface *sur = get_surface(get_address (msg));
2093         boost::shared_ptr<Stripable> s;
2094         if (sur->expand_enable) {
2095                 s = get_strip (sur->expand, get_address (msg));
2096         } else {
2097                 s = _select;
2098         }
2099         if (s) {
2100                 if (s->phase_control()) {
2101                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2102                         return 0;
2103                 }
2104         }
2105         return sel_fail ("polarity", 0, get_address (msg));
2106 }
2107
2108 int
2109 OSC::strip_expand (int ssid, int yn, lo_message msg)
2110 {
2111         OSCSurface *sur = get_surface(get_address (msg));
2112         sur->expand_enable = (bool) yn;
2113         sur->expand = ssid;
2114         boost::shared_ptr<Stripable> s;
2115         if (yn) {
2116                 s = get_strip (ssid, get_address (msg));
2117         } else {
2118                 s = ControlProtocol::first_selected_stripable();
2119         }
2120
2121         return _strip_select (s, get_address (msg));
2122 }
2123
2124 int
2125 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2126 {
2127         if (!session) {
2128                 return -1;
2129         }
2130         OSCSurface *sur = get_surface(addr);
2131         if (sur->sel_obs) {
2132                 delete sur->sel_obs;
2133                 sur->sel_obs = 0;
2134         }
2135         if (s) {
2136                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2137                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2138                 sur->sel_obs = sel_fb;
2139         } else if (sur->expand_enable) {
2140                 sur->expand = 0;
2141                 sur->expand_enable = false;
2142                 if (_select) {
2143                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2144                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2145                         sur->sel_obs = sel_fb;
2146                 }
2147         } else {
2148                 route_send_fail ("select", sur->expand, 0 , addr);
2149         }
2150         //update buttons on surface
2151         int b_s = sur->bank_size;
2152         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2153                 b_s = sur->nstrips;
2154         }
2155         for (int i = 1;  i <= b_s; i++) {
2156                 string path = "expand";
2157
2158                 if ((i == (int) sur->expand) && sur->expand_enable) {
2159                         lo_message reply = lo_message_new ();
2160                         if (sur->feedback[2]) {
2161                                 ostringstream os;
2162                                 os << "/strip/" << path << "/" << i;
2163                                 path = os.str();
2164                         } else {
2165                                 ostringstream os;
2166                                 os << "/strip/" << path;
2167                                 path = os.str();
2168                                 lo_message_add_int32 (reply, i);
2169                         }
2170                         lo_message_add_float (reply, (float) 1);
2171
2172                         lo_send_message (addr, path.c_str(), reply);
2173                         lo_message_free (reply);
2174                         reply = lo_message_new ();
2175                         lo_message_add_float (reply, 1.0);
2176                         lo_send_message (addr, "/select/expand", reply);
2177                         lo_message_free (reply);
2178
2179                 } else {
2180                         lo_message reply = lo_message_new ();
2181                         lo_message_add_int32 (reply, i);
2182                         lo_message_add_float (reply, 0.0);
2183                         lo_send_message (addr, "/strip/expand", reply);
2184                         lo_message_free (reply);
2185                 }
2186         }
2187         if (!sur->expand_enable) {
2188                 lo_message reply = lo_message_new ();
2189                 lo_message_add_float (reply, 0.0);
2190                 lo_send_message (addr, "/select/expand", reply);
2191                 lo_message_free (reply);
2192         }
2193
2194         return 0;
2195 }
2196
2197 int
2198 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2199 {
2200         //ignore button release
2201         if (!yn) return 0;
2202
2203         if (!session) {
2204                 route_send_fail ("select", ssid, 0, get_address (msg));
2205                 return -1;
2206         }
2207         OSCSurface *sur = get_surface(get_address (msg));
2208         sur->expand_enable = false;
2209         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2210         if (s) {
2211                 SetStripableSelection (s);
2212         } else {
2213                 route_send_fail ("select", ssid, 0, get_address (msg));
2214         }
2215
2216         return 0;
2217 }
2218
2219 int
2220 OSC::sel_expand (uint32_t state, lo_message msg)
2221 {
2222         OSCSurface *sur = get_surface(get_address (msg));
2223         boost::shared_ptr<Stripable> s;
2224         sur->expand_enable = (bool) state;
2225         if (state && sur->expand) {
2226                 s = get_strip (sur->expand, get_address (msg));
2227         } else {
2228                 s = ControlProtocol::first_selected_stripable();
2229         }
2230
2231         return _strip_select (s, get_address (msg));
2232 }
2233
2234 int
2235 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2236 {
2237         if (!session) return -1;
2238         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2239
2240         if (s) {
2241                 if (s->gain_control()) {
2242                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2243                 } else {
2244                         return 1;
2245                 }
2246         } else {
2247                 return 1;
2248         }
2249
2250         return 0;
2251 }
2252
2253 int
2254 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2255 {
2256         if (!session) {
2257                 route_send_fail ("gain", ssid, -193, get_address (msg));
2258                 return -1;
2259         }
2260         int ret;
2261         if (dB < -192) {
2262                 ret = route_set_gain_abs (ssid, 0.0, msg);
2263         } else {
2264                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2265         }
2266         if (ret != 0) {
2267                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2268         }
2269         return 0;
2270 }
2271
2272 int
2273 OSC::sel_gain (float val, lo_message msg)
2274 {
2275         OSCSurface *sur = get_surface(get_address (msg));
2276         boost::shared_ptr<Stripable> s;
2277         if (sur->expand_enable) {
2278                 s = get_strip (sur->expand, get_address (msg));
2279         } else {
2280                 s = _select;
2281         }
2282         if (s) {
2283                 float abs;
2284                 if (val < -192) {
2285                         abs = 0;
2286                 } else {
2287                         abs = dB_to_coefficient (val);
2288                 }
2289                 if (s->gain_control()) {
2290                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2291                         return 0;
2292                 }
2293         }
2294         return sel_fail ("gain", -193, get_address (msg));
2295 }
2296
2297 int
2298 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2299 {
2300         if (!session) {
2301                 route_send_fail ("fader", ssid, 0, get_address (msg));
2302                 return -1;
2303         }
2304         int ret;
2305         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2306         if (ret != 0) {
2307                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2308         }
2309         return 0;
2310 }
2311
2312 int
2313 OSC::sel_fader (float val, lo_message msg)
2314 {
2315         OSCSurface *sur = get_surface(get_address (msg));
2316         boost::shared_ptr<Stripable> s;
2317         if (sur->expand_enable) {
2318                 s = get_strip (sur->expand, get_address (msg));
2319         } else {
2320                 s = _select;
2321         }
2322         if (s) {
2323                 float abs;
2324                 abs = slider_position_to_gain_with_max (val, 2.0);
2325                 if (s->gain_control()) {
2326                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2327                         return 0;
2328                 }
2329         }
2330         return sel_fail ("fader", 0, get_address (msg));
2331 }
2332
2333 int
2334 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2335 {
2336         if (!session) return -1;
2337         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2338
2339         if (s) {
2340                 if (s->trim_control()) {
2341                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2342                         return 0;
2343                 }
2344
2345         }
2346
2347         return -1;
2348 }
2349
2350 int
2351 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2352 {
2353         int ret;
2354         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2355         if (ret != 0) {
2356                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2357         }
2358
2359 return 0;
2360 }
2361
2362 int
2363 OSC::sel_trim (float val, lo_message msg)
2364 {
2365         OSCSurface *sur = get_surface(get_address (msg));
2366         boost::shared_ptr<Stripable> s;
2367         if (sur->expand_enable) {
2368                 s = get_strip (sur->expand, get_address (msg));
2369         } else {
2370                 s = _select;
2371         }
2372         if (s) {
2373                 if (s->trim_control()) {
2374                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2375                         return 0;
2376                 }
2377         }
2378         return sel_fail ("trimdB", 0, get_address (msg));
2379 }
2380
2381 int
2382 OSC::sel_pan_position (float val, lo_message msg)
2383 {
2384         OSCSurface *sur = get_surface(get_address (msg));
2385         boost::shared_ptr<Stripable> s;
2386         if (sur->expand_enable) {
2387                 s = get_strip (sur->expand, get_address (msg));
2388         } else {
2389                 s = _select;
2390         }
2391         if (s) {
2392                 if(s->pan_azimuth_control()) {
2393                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2394                         return sel_fail ("pan_stereo_position", s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ()), get_address (msg));
2395                         return 0;
2396                 }
2397         }
2398         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2399 }
2400
2401 int
2402 OSC::sel_pan_width (float val, lo_message msg)
2403 {
2404         OSCSurface *sur = get_surface(get_address (msg));
2405         boost::shared_ptr<Stripable> s;
2406         if (sur->expand_enable) {
2407                 s = get_strip (sur->expand, get_address (msg));
2408         } else {
2409                 s = _select;
2410         }
2411         if (s) {
2412                 if (s->pan_width_control()) {
2413                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2414                         return 0;
2415                 }
2416         }
2417         return sel_fail ("pan_stereo_width", 1, get_address (msg));
2418 }
2419
2420 int
2421 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
2422 {
2423         if (!session) return -1;
2424         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2425
2426         if (s) {
2427                 if(s->pan_azimuth_control()) {
2428                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
2429                         return route_send_fail ("pan_stereo_position", ssid, s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ()), get_address (msg));
2430                 }
2431         }
2432
2433         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
2434 }
2435
2436 int
2437 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
2438 {
2439         if (!session) return -1;
2440         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2441
2442         if (s) {
2443                 if (s->pan_width_control()) {
2444                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
2445                         return 0;
2446                 }
2447         }
2448
2449         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
2450 }
2451
2452 int
2453 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
2454 {
2455         if (!session) {
2456                 return -1;
2457         }
2458         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2459         float abs;
2460         if (s) {
2461                 if (id > 0) {
2462                         --id;
2463                 }
2464 #ifdef MIXBUS
2465                 abs = val;
2466 #else
2467                 if (val < -192) {
2468                         abs = 0;
2469                 } else {
2470                         abs = dB_to_coefficient (val);
2471                 }
2472 #endif
2473                 if (s->send_level_controllable (id)) {
2474                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2475                         return 0;
2476                 }
2477         }
2478         return 0;
2479 }
2480
2481 int
2482 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
2483 {
2484         if (!session) {
2485                 return -1;
2486         }
2487         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2488         float abs;
2489         if (s) {
2490
2491                 if (id > 0) {
2492                         --id;
2493                 }
2494
2495                 if (s->send_level_controllable (id)) {
2496 #ifdef MIXBUS
2497                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2498 #else
2499                         abs = slider_position_to_gain_with_max (val, 2.0);
2500 #endif
2501                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2502                         return 0;
2503                 }
2504         }
2505         return 0;
2506 }
2507
2508 int
2509 OSC::sel_sendgain (int id, float val, lo_message msg)
2510 {
2511         OSCSurface *sur = get_surface(get_address (msg));
2512         boost::shared_ptr<Stripable> s;
2513         if (sur->expand_enable) {
2514                 s = get_strip (sur->expand, get_address (msg));
2515         } else {
2516                 s = _select;
2517         }
2518         float abs;
2519         if (s) {
2520                 if (id > 0) {
2521                         --id;
2522                 }
2523 #ifdef MIXBUS
2524                 abs = val;
2525 #else
2526                 if (val < -192) {
2527                         abs = 0;
2528                 } else {
2529                         abs = dB_to_coefficient (val);
2530                 }
2531 #endif
2532                 if (s->send_level_controllable (id)) {
2533                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2534                         return 0;
2535                 }
2536         }
2537         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
2538 }
2539
2540 int
2541 OSC::sel_sendfader (int id, float val, lo_message msg)
2542 {
2543         OSCSurface *sur = get_surface(get_address (msg));
2544         boost::shared_ptr<Stripable> s;
2545         if (sur->expand_enable) {
2546                 s = get_strip (sur->expand, get_address (msg));
2547         } else {
2548                 s = _select;
2549         }
2550         float abs;
2551         if (s) {
2552
2553                 if (id > 0) {
2554                         --id;
2555                 }
2556
2557                 if (s->send_level_controllable (id)) {
2558 #ifdef MIXBUS
2559                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2560 #else
2561                         abs = slider_position_to_gain_with_max (val, 2.0);
2562 #endif
2563                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2564                         return 0;
2565                 }
2566         }
2567         return sel_send_fail ("send_fader", id, 0, get_address (msg));
2568 }
2569
2570 int
2571 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
2572 {
2573         if (!session) {
2574                 return -1;
2575         }
2576         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2577
2578         if (s) {
2579
2580                 /* revert to zero-based counting */
2581
2582                 if (sid > 0) {
2583                         --sid;
2584                 }
2585
2586                 if (s->send_enable_controllable (sid)) {
2587                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
2588                         return 0;
2589                 }
2590
2591                 if (s->send_level_controllable (sid)) {
2592                         return 0;
2593                 }
2594
2595         }
2596
2597         return -1;
2598 }
2599
2600 int
2601 OSC::sel_sendenable (int id, float val, lo_message msg)
2602 {
2603         OSCSurface *sur = get_surface(get_address (msg));
2604         boost::shared_ptr<Stripable> s;
2605         if (sur->expand_enable) {
2606                 s = get_strip (sur->expand, get_address (msg));
2607         } else {
2608                 s = _select;
2609         }
2610         if (s) {
2611                 if (id > 0) {
2612                         --id;
2613                 }
2614                 if (s->send_enable_controllable (id)) {
2615                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
2616                         return 0;
2617                 }
2618                 if (s->send_level_controllable (id)) {
2619                         return sel_send_fail ("send_enable", id + 1, 1, get_address (msg));
2620                 }
2621         }
2622         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
2623 }
2624
2625 int
2626 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
2627 {
2628         if (!session)
2629                 return -1;
2630         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2631
2632         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2633
2634         if (!r) {
2635                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2636                 return -1;
2637         }
2638
2639         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
2640
2641         if (!redi) {
2642                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2643                 return -1;
2644         }
2645
2646         boost::shared_ptr<PluginInsert> pi;
2647
2648         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2649                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2650                 return -1;
2651         }
2652
2653         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2654         bool ok=false;
2655
2656         uint32_t controlid = pip->nth_parameter (par,ok);
2657
2658         if (!ok) {
2659                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
2660                 return -1;
2661         }
2662
2663         if (!pip->parameter_is_input(controlid)) {
2664                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
2665                 return -1;
2666         }
2667
2668         ParameterDescriptor pd;
2669         pi->plugin()->get_parameter_descriptor (controlid,pd);
2670
2671         if (val >= pd.lower && val <= pd.upper) {
2672
2673                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
2674                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
2675                 c->set_value (val, PBD::Controllable::NoGroup);
2676         } else {
2677                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
2678                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
2679         }
2680
2681         return 0;
2682 }
2683
2684 //prints to cerr only
2685 int
2686 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
2687 {
2688         if (!session) {
2689                 return -1;
2690         }
2691         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2692
2693         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2694
2695         if (!r) {
2696                 return -1;
2697         }
2698
2699         boost::shared_ptr<Processor> redi=r->nth_processor (piid);
2700
2701         if (!redi) {
2702                 return -1;
2703         }
2704
2705         boost::shared_ptr<PluginInsert> pi;
2706
2707         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2708                 return -1;
2709         }
2710
2711         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2712         bool ok=false;
2713
2714         uint32_t controlid = pip->nth_parameter (par,ok);
2715
2716         if (!ok) {
2717                 return -1;
2718         }
2719
2720         ParameterDescriptor pd;
2721
2722         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
2723                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
2724
2725                 cerr << "parameter:     " << redi->describe_parameter(controlid)  << "\n";
2726                 cerr << "current value: " << c->get_value ();
2727                 cerr << "lower value:   " << pd.lower << "\n";
2728                 cerr << "upper value:   " << pd.upper << "\n";
2729         }
2730
2731         return 0;
2732 }
2733
2734 int
2735 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
2736 {
2737         if (!session)
2738                 return -1;
2739         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2740
2741         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2742
2743         if (!r) {
2744                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2745                 return -1;
2746         }
2747
2748         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
2749
2750         if (!redi) {
2751                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2752                 return -1;
2753         }
2754
2755         boost::shared_ptr<PluginInsert> pi;
2756
2757         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2758                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2759                 return -1;
2760         }
2761
2762         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2763         pi->activate();
2764
2765         return 0;
2766 }
2767
2768 int
2769 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
2770 {
2771         if (!session)
2772                 return -1;
2773         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2774
2775         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2776
2777         if (!r) {
2778                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2779                 return -1;
2780         }
2781
2782         boost::shared_ptr<Processor> redi=r->nth_plugin (piid);
2783
2784         if (!redi) {
2785                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2786                 return -1;
2787         }
2788
2789         boost::shared_ptr<PluginInsert> pi;
2790
2791         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2792                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2793                 return -1;
2794         }
2795
2796         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2797         pi->deactivate();
2798
2799         return 0;
2800 }
2801
2802 // select
2803
2804 int
2805 OSC::sel_pan_elevation (float val, lo_message msg)
2806 {
2807         OSCSurface *sur = get_surface(get_address (msg));
2808         boost::shared_ptr<Stripable> s;
2809         if (sur->expand_enable) {
2810                 s = get_strip (sur->expand, get_address (msg));
2811         } else {
2812                 s = _select;
2813         }
2814         if (s) {
2815                 if (s->pan_elevation_control()) {
2816                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2817                         return 0;
2818                 }
2819         }
2820         return sel_fail ("pan_elevation_position", 0, get_address (msg));
2821 }
2822
2823 int
2824 OSC::sel_pan_frontback (float val, lo_message msg)
2825 {
2826         OSCSurface *sur = get_surface(get_address (msg));
2827         boost::shared_ptr<Stripable> s;
2828         if (sur->expand_enable) {
2829                 s = get_strip (sur->expand, get_address (msg));
2830         } else {
2831                 s = _select;
2832         }
2833         if (s) {
2834                 if (s->pan_frontback_control()) {
2835                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2836                         return 0;
2837                 }
2838         }
2839         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
2840 }
2841
2842 int
2843 OSC::sel_pan_lfe (float val, lo_message msg)
2844 {
2845         OSCSurface *sur = get_surface(get_address (msg));
2846         boost::shared_ptr<Stripable> s;
2847         if (sur->expand_enable) {
2848                 s = get_strip (sur->expand, get_address (msg));
2849         } else {
2850                 s = _select;
2851         }
2852         if (s) {
2853                 if (s->pan_lfe_control()) {
2854                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2855                         return 0;
2856                 }
2857         }
2858         return sel_fail ("pan_lfe_control", 0, get_address (msg));
2859 }
2860
2861 // compressor control
2862 int
2863 OSC::sel_comp_enable (float val, lo_message msg)
2864 {
2865         OSCSurface *sur = get_surface(get_address (msg));
2866         boost::shared_ptr<Stripable> s;
2867         if (sur->expand_enable) {
2868                 s = get_strip (sur->expand, get_address (msg));
2869         } else {
2870                 s = _select;
2871         }
2872         if (s) {
2873                 if (s->comp_enable_controllable()) {
2874                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2875                         return 0;
2876                 }
2877         }
2878         return sel_fail ("comp_enable", 0, get_address (msg));
2879 }
2880
2881 int
2882 OSC::sel_comp_threshold (float val, lo_message msg)
2883 {
2884         OSCSurface *sur = get_surface(get_address (msg));
2885         boost::shared_ptr<Stripable> s;
2886         if (sur->expand_enable) {
2887                 s = get_strip (sur->expand, get_address (msg));
2888         } else {
2889                 s = _select;
2890         }
2891         if (s) {
2892                 if (s->comp_threshold_controllable()) {
2893                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2894                         return 0;
2895                 }
2896         }
2897         return sel_fail ("comp_threshold", 0, get_address (msg));
2898 }
2899
2900 int
2901 OSC::sel_comp_speed (float val, lo_message msg)
2902 {
2903         OSCSurface *sur = get_surface(get_address (msg));
2904         boost::shared_ptr<Stripable> s;
2905         if (sur->expand_enable) {
2906                 s = get_strip (sur->expand, get_address (msg));
2907         } else {
2908                 s = _select;
2909         }
2910         if (s) {
2911                 if (s->comp_speed_controllable()) {
2912                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2913                         return 0;
2914                 }
2915         }
2916         return sel_fail ("comp_speed", 0, get_address (msg));
2917 }
2918
2919 int
2920 OSC::sel_comp_mode (float val, lo_message msg)
2921 {
2922         OSCSurface *sur = get_surface(get_address (msg));
2923         boost::shared_ptr<Stripable> s;
2924         if (sur->expand_enable) {
2925                 s = get_strip (sur->expand, get_address (msg));
2926         } else {
2927                 s = _select;
2928         }
2929         if (s) {
2930                 if (s->comp_mode_controllable()) {
2931                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2932                         return 0;
2933                 }
2934         }
2935         return sel_fail ("comp_mode", 0, get_address (msg));
2936 }
2937
2938 int
2939 OSC::sel_comp_makeup (float val, lo_message msg)
2940 {
2941         OSCSurface *sur = get_surface(get_address (msg));
2942         boost::shared_ptr<Stripable> s;
2943         if (sur->expand_enable) {
2944                 s = get_strip (sur->expand, get_address (msg));
2945         } else {
2946                 s = _select;
2947         }
2948         if (s) {
2949                 if (s->comp_makeup_controllable()) {
2950                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2951                         return 0;
2952                 }
2953         }
2954         return sel_fail ("comp_makeup", 0, get_address (msg));
2955 }
2956
2957 // EQ control
2958
2959 int
2960 OSC::sel_eq_enable (float val, lo_message msg)
2961 {
2962         OSCSurface *sur = get_surface(get_address (msg));
2963         boost::shared_ptr<Stripable> s;
2964         if (sur->expand_enable) {
2965                 s = get_strip (sur->expand, get_address (msg));
2966         } else {
2967                 s = _select;
2968         }
2969         if (s) {
2970                 if (s->eq_enable_controllable()) {
2971                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2972                         return 0;
2973                 }
2974         }
2975         return sel_fail ("eq_enable", 0, get_address (msg));
2976 }
2977
2978 int
2979 OSC::sel_eq_hpf (float val, lo_message msg)
2980 {
2981         OSCSurface *sur = get_surface(get_address (msg));
2982         boost::shared_ptr<Stripable> s;
2983         if (sur->expand_enable) {
2984                 s = get_strip (sur->expand, get_address (msg));
2985         } else {
2986                 s = _select;
2987         }
2988         if (s) {
2989                 if (s->eq_hpf_controllable()) {
2990                         s->eq_hpf_controllable()->set_value (s->eq_hpf_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
2991                         return 0;
2992                 }
2993         }
2994         return sel_fail ("eq_hpf", 0, get_address (msg));
2995 }
2996
2997 int
2998 OSC::sel_eq_gain (int id, float val, lo_message msg)
2999 {
3000         OSCSurface *sur = get_surface(get_address (msg));
3001         boost::shared_ptr<Stripable> s;
3002         if (sur->expand_enable) {
3003                 s = get_strip (sur->expand, get_address (msg));
3004         } else {
3005                 s = _select;
3006         }
3007         if (s) {
3008                 if (id > 0) {
3009                         --id;
3010                 }
3011                 if (s->eq_gain_controllable (id)) {
3012                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3013                         return 0;
3014                 }
3015         }
3016         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3017 }
3018
3019 int
3020 OSC::sel_eq_freq (int id, float val, lo_message msg)
3021 {
3022         OSCSurface *sur = get_surface(get_address (msg));
3023         boost::shared_ptr<Stripable> s;
3024         if (sur->expand_enable) {
3025                 s = get_strip (sur->expand, get_address (msg));
3026         } else {
3027                 s = _select;
3028         }
3029         if (s) {
3030                 if (id > 0) {
3031                         --id;
3032                 }
3033                 if (s->eq_freq_controllable (id)) {
3034                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3035                         return 0;
3036                 }
3037         }
3038         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3039 }
3040
3041 int
3042 OSC::sel_eq_q (int id, float val, lo_message msg)
3043 {
3044         OSCSurface *sur = get_surface(get_address (msg));
3045         boost::shared_ptr<Stripable> s;
3046         if (sur->expand_enable) {
3047                 s = get_strip (sur->expand, get_address (msg));
3048         } else {
3049                 s = _select;
3050         }
3051         if (s) {
3052                 if (id > 0) {
3053                         --id;
3054                 }
3055                 if (s->eq_q_controllable (id)) {
3056                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3057                         return 0;
3058                 }
3059         }
3060         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3061 }
3062
3063 int
3064 OSC::sel_eq_shape (int id, float val, lo_message msg)
3065 {
3066         OSCSurface *sur = get_surface(get_address (msg));
3067         boost::shared_ptr<Stripable> s;
3068         if (sur->expand_enable) {
3069                 s = get_strip (sur->expand, get_address (msg));
3070         } else {
3071                 s = _select;
3072         }
3073         if (s) {
3074                 if (id > 0) {
3075                         --id;
3076                 }
3077                 if (s->eq_shape_controllable (id)) {
3078                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3079                         return 0;
3080                 }
3081         }
3082         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3083 }
3084
3085 void
3086 OSC::gui_selection_changed ()
3087 {
3088         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3089
3090         if (strip) {
3091                 _select = strip;
3092                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3093                         OSCSurface* sur = &_surface[it];
3094                         if(!sur->expand_enable) {
3095                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3096                                 _strip_select (strip, addr);
3097                         }
3098                 }
3099         }
3100 }
3101
3102 // timer callbacks
3103 bool
3104 OSC::periodic (void)
3105 {
3106         if (!tick) {
3107                 Glib::usleep(100); // let flurry of signals subside
3108                 if (global_init) {
3109                         for (uint32_t it = 0; it < _surface.size(); it++) {
3110                                 OSCSurface* sur = &_surface[it];
3111                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3112                                 global_feedback (sur->feedback, addr, sur->gainmode);
3113                         }
3114                         global_init = false;
3115                         tick = true;
3116                 }
3117                 if (bank_dirty) {
3118                         _recalcbanks ();
3119                         bank_dirty = false;
3120                         tick = true;
3121                 }
3122         }
3123
3124         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3125
3126                 OSCGlobalObserver* go;
3127
3128                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3129                         go->tick();
3130                 }
3131         }
3132         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3133
3134                 OSCRouteObserver* ro;
3135
3136                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3137                         ro->tick();
3138                 }
3139         }
3140         for (uint32_t it = 0; it < _surface.size(); it++) {
3141                 OSCSurface* sur = &_surface[it];
3142                 OSCSelectObserver* so;
3143                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3144                         so->tick();
3145                 }
3146         }
3147         return true;
3148 }
3149
3150 int
3151 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3152 {
3153         OSCSurface *sur = get_surface(addr);
3154
3155         ostringstream os;
3156         lo_message reply;
3157         if (ssid) {
3158                 reply = lo_message_new ();
3159                 if (sur->feedback[2]) {
3160                         os << "/strip/" << path << "/" << ssid;
3161                 } else {
3162                         os << "/strip/" << path;
3163                         lo_message_add_int32 (reply, ssid);
3164                 }
3165                 string str_pth = os.str();
3166                 lo_message_add_float (reply, (float) val);
3167
3168                 lo_send_message (addr, str_pth.c_str(), reply);
3169                 lo_message_free (reply);
3170         }
3171         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3172                 os.str("");
3173                 os << "/select/" << path;
3174                 string sel_pth = os.str();
3175                 reply = lo_message_new ();
3176                 lo_message_add_float (reply, (float) val);
3177                 lo_send_message (addr, sel_pth.c_str(), reply);
3178                 lo_message_free (reply);
3179         }
3180
3181         return 0;
3182 }
3183
3184 int
3185 OSC::sel_fail (string path, float val, lo_address addr)
3186 {
3187         ostringstream os;
3188         os.str("");
3189         os << "/select/" << path;
3190         string sel_pth = os.str();
3191         lo_message reply = lo_message_new ();
3192         lo_message_add_float (reply, (float) val);
3193         lo_send_message (addr, sel_pth.c_str(), reply);
3194         lo_message_free (reply);
3195
3196         return 0;
3197 }
3198
3199 int
3200 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
3201 {
3202         OSCSurface *sur = get_surface(addr);
3203
3204         ostringstream os;
3205         lo_message reply;
3206         reply = lo_message_new ();
3207         if (sur->feedback[2]) {
3208                 os << "/select/" << path << "/" << id;
3209         } else {
3210                 os << "/select/" << path;
3211                 lo_message_add_int32 (reply, id);
3212         }
3213         string str_pth = os.str();
3214         lo_message_add_float (reply, (float) val);
3215
3216         lo_send_message (addr, str_pth.c_str(), reply);
3217         lo_message_free (reply);
3218
3219         return 0;
3220 }
3221
3222 XMLNode&
3223 OSC::get_state ()
3224 {
3225         XMLNode& node (ControlProtocol::get_state());
3226         node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
3227         node.add_property ("address-only", address_only);
3228         node.add_property ("remote-port", remote_port);
3229         node.add_property ("banksize", default_banksize);
3230         node.add_property ("striptypes", default_strip);
3231         node.add_property ("feedback", default_feedback);
3232         node.add_property ("gainmode", default_gainmode);
3233         if (_surface.size()) {
3234                 XMLNode* config = new XMLNode (X_("Configurations"));
3235                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3236                         OSCSurface* sur = &_surface[it];
3237                         XMLNode* devnode = new XMLNode (X_("Configuration"));
3238                         devnode->add_property (X_("url"), sur->remote_url);
3239                         devnode->add_property (X_("bank-size"), sur->bank_size);
3240                         devnode->add_property (X_("strip-types"), sur->strip_types.to_ulong());
3241                         devnode->add_property (X_("feedback"), sur->feedback.to_ulong());
3242                         devnode->add_property (X_("gainmode"), sur->gainmode);
3243                         config->add_child_nocopy (*devnode);
3244                 }
3245                 node.add_child_nocopy (*config);
3246         }
3247         return node;
3248 }
3249
3250 int
3251 OSC::set_state (const XMLNode& node, int version)
3252 {
3253         if (ControlProtocol::set_state (node, version)) {
3254                 return -1;
3255         }
3256         XMLProperty const * p = node.property (X_("debugmode"));
3257         if (p) {
3258                 _debugmode = OSCDebugMode (PBD::atoi(p->value ()));
3259         }
3260         p = node.property (X_("address-only"));
3261         if (p) {
3262                 address_only = OSCDebugMode (PBD::atoi(p->value ()));
3263         }
3264         p = node.property (X_("remote-port"));
3265         if (p) {
3266                 remote_port = p->value ();
3267         }
3268         p = node.property (X_("banksize"));
3269         if (p) {
3270                 default_banksize = OSCDebugMode (PBD::atoi(p->value ()));
3271         }
3272         p = node.property (X_("striptypes"));
3273         if (p) {
3274                 default_strip = OSCDebugMode (PBD::atoi(p->value ()));
3275         }
3276         p = node.property (X_("feedback"));
3277         if (p) {
3278                 default_feedback = OSCDebugMode (PBD::atoi(p->value ()));
3279         }
3280         p = node.property (X_("gainmode"));
3281         if (p) {
3282                 default_gainmode = OSCDebugMode (PBD::atoi(p->value ()));
3283         }
3284         XMLNode* cnode = node.child (X_("Configurations"));
3285
3286         if (cnode) {
3287                 XMLNodeList const& devices = cnode->children();
3288                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
3289                         XMLProperty const * prop = (*d)->property (X_("url"));
3290                         if (prop) {
3291                                 OSCSurface s;
3292                                 bank_dirty = true;
3293                                 s.remote_url = prop->value();
3294                                 prop = (*d)->property (X_("bank-size"));
3295                                 if (prop) {
3296                                         s.bank_size = atoi (prop->value().c_str());
3297                                 }
3298                                 prop = (*d)->property (X_("strip-types"));
3299                                 if (prop) {
3300                                         s.strip_types = atoi (prop->value().c_str());
3301                                 }
3302                                 prop = (*d)->property (X_("feedback"));
3303                                 if (prop) {
3304                                         s.feedback = atoi (prop->value().c_str());
3305                                 }
3306                                 prop = (*d)->property (X_("gainmode"));
3307                                 if (prop) {
3308                                         s.gainmode = atoi (prop->value().c_str());
3309                                 }
3310                                 s.bank = 1;
3311                                 s.sel_obs = 0;
3312                                 s.expand = 0;
3313                                 s.expand_enable = false;
3314                                 s.strips = get_sorted_stripables(s.strip_types);
3315                                 s.nstrips = s.strips.size();
3316                                 _surface.push_back (s);
3317                         }
3318                 }
3319         }
3320         global_init = true;
3321         tick = false;
3322
3323         return 0;
3324 }
3325
3326 // predicate for sort call in get_sorted_stripables
3327 struct StripableByPresentationOrder
3328 {
3329         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
3330         {
3331                 return a->presentation_info().order() < b->presentation_info().order();
3332         }
3333
3334         bool operator () (const Stripable & a, const Stripable & b) const
3335         {
3336                 return a.presentation_info().order() < b.presentation_info().order();
3337         }
3338
3339         bool operator () (const Stripable * a, const Stripable * b) const
3340         {
3341                 return a->presentation_info().order() < b->presentation_info().order();
3342         }
3343 };
3344
3345 OSC::Sorted
3346 OSC::get_sorted_stripables(std::bitset<32> types)
3347 {
3348         Sorted sorted;
3349
3350         // fetch all stripables
3351         StripableList stripables;
3352
3353         session->get_stripables (stripables);
3354
3355         // Look for stripables that match bit in sur->strip_types
3356         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
3357
3358                 boost::shared_ptr<Stripable> s = *it;
3359                 if ((!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3360                         // do nothing... skip it
3361                 } else {
3362
3363                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
3364                                 sorted.push_back (s);
3365                         } else
3366                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
3367                                 sorted.push_back (s);
3368                         } else
3369                         if (types[2] && (s->presentation_info().flags() & PresentationInfo::AudioBus)) {
3370                                 sorted.push_back (s);
3371                         } else
3372                         if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
3373                                 sorted.push_back (s);
3374                         } else
3375                         if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
3376                                 sorted.push_back (s);
3377                         } else
3378                         if (types[8] && (s->presentation_info().flags() & PresentationInfo::Selected)) {
3379                                 sorted.push_back (s);
3380                         } else
3381                         if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3382                                 sorted.push_back (s);
3383                         }
3384                 }
3385         }
3386         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
3387         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
3388         if (types[5]) {
3389                 sorted.push_back (session->master_out());
3390         }
3391         if (types[6]) {
3392                 sorted.push_back (session->monitor_out());
3393         }
3394         return sorted;
3395 }
3396