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