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