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