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