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