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