OSC: Solo toggle should maintain state when locked, rec and rec_safe should show...
[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 route_send_fail ("solo", ssid, (float) s->solo_control()->get_value(), get_address (msg));
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 sel_fail ("solo", (float) s->solo_control()->get_value(), get_address (msg));
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                         if (s->rec_enable_control()->get_value()) {
2063                                 return 0;
2064                         }
2065                 }
2066         }
2067         return sel_fail ("recenable", 0, get_address (msg));
2068 }
2069
2070 int
2071 OSC::route_recenable (int ssid, int yn, lo_message msg)
2072 {
2073         if (!session) return -1;
2074         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2075
2076         if (s) {
2077                 if (s->rec_enable_control()) {
2078                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2079                         if (s->rec_enable_control()->get_value()) {
2080                                 return 0;
2081                         }
2082                 }
2083         }
2084         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2085 }
2086
2087 int
2088 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2089     if (!session) {
2090         return -1;
2091     }
2092
2093     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2094
2095     if (s) {
2096         s->set_name(std::string(newname));
2097     }
2098
2099     return 0;
2100 }
2101
2102 int
2103 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2104 {
2105         OSCSurface *sur = get_surface(get_address (msg));
2106         boost::shared_ptr<Stripable> s;
2107         if (sur->expand_enable) {
2108                 s = get_strip (sur->expand, get_address (msg));
2109         } else {
2110                 s = _select;
2111         }
2112         if (s) {
2113                 if (s->rec_safe_control()) {
2114                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2115                         if (s->rec_safe_control()->get_value()) {
2116                                 return 0;
2117                         }
2118                 }
2119         }
2120         return sel_fail ("record_safe", 0, get_address (msg));
2121 }
2122
2123 int
2124 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2125 {
2126         if (!session) return -1;
2127         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2128         if (s) {
2129                 if (s->rec_safe_control()) {
2130                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2131                         if (s->rec_safe_control()->get_value()) {
2132                                 return 0;
2133                         }
2134                 }
2135         }
2136         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2137 }
2138
2139 int
2140 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2141 {
2142         if (!session) return -1;
2143         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2144
2145         if (s) {
2146                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2147                 if (track) {
2148                         if (track->monitoring_control()) {
2149                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2150                                 return 0;
2151                         }
2152                 }
2153         }
2154
2155         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2156 }
2157
2158 int
2159 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2160 {
2161         OSCSurface *sur = get_surface(get_address (msg));
2162         boost::shared_ptr<Stripable> s;
2163         if (sur->expand_enable) {
2164                 s = get_strip (sur->expand, get_address (msg));
2165         } else {
2166                 s = _select;
2167         }
2168         if (s) {
2169                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2170                 if (track) {
2171                         if (track->monitoring_control()) {
2172                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2173                                 return 0;
2174                         }
2175                 }
2176         }
2177         return sel_fail ("monitor_input", 0, get_address (msg));
2178 }
2179
2180 int
2181 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2182 {
2183         if (!session) return -1;
2184         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2185
2186         if (s) {
2187                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2188                 if (track) {
2189                         if (track->monitoring_control()) {
2190                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2191                                 return 0;
2192                         }
2193                 }
2194         }
2195
2196         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2197 }
2198
2199 int
2200 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2201 {
2202         OSCSurface *sur = get_surface(get_address (msg));
2203         boost::shared_ptr<Stripable> s;
2204         if (sur->expand_enable) {
2205                 s = get_strip (sur->expand, get_address (msg));
2206         } else {
2207                 s = _select;
2208         }
2209         if (s) {
2210                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2211                 if (track) {
2212                         if (track->monitoring_control()) {
2213                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2214                                 return 0;
2215                         }
2216                 }
2217         }
2218         return sel_fail ("monitor_disk", 0, get_address (msg));
2219 }
2220
2221
2222 int
2223 OSC::strip_phase (int ssid, int yn, lo_message msg)
2224 {
2225         if (!session) return -1;
2226         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2227
2228         if (s) {
2229                 if (s->phase_control()) {
2230                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2231                         return 0;
2232                 }
2233         }
2234
2235         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2236 }
2237
2238 int
2239 OSC::sel_phase (uint32_t yn, lo_message msg)
2240 {
2241         OSCSurface *sur = get_surface(get_address (msg));
2242         boost::shared_ptr<Stripable> s;
2243         if (sur->expand_enable) {
2244                 s = get_strip (sur->expand, get_address (msg));
2245         } else {
2246                 s = _select;
2247         }
2248         if (s) {
2249                 if (s->phase_control()) {
2250                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2251                         return 0;
2252                 }
2253         }
2254         return sel_fail ("polarity", 0, get_address (msg));
2255 }
2256
2257 int
2258 OSC::strip_expand (int ssid, int yn, lo_message msg)
2259 {
2260         OSCSurface *sur = get_surface(get_address (msg));
2261         sur->expand_enable = (bool) yn;
2262         sur->expand = ssid;
2263         boost::shared_ptr<Stripable> s;
2264         if (yn) {
2265                 s = get_strip (ssid, get_address (msg));
2266         } else {
2267                 s = ControlProtocol::first_selected_stripable();
2268         }
2269
2270         return _strip_select (s, get_address (msg));
2271 }
2272
2273 int
2274 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2275 {
2276         if (!session) {
2277                 return -1;
2278         }
2279         OSCSurface *sur = get_surface(addr);
2280         if (sur->sel_obs) {
2281                 delete sur->sel_obs;
2282                 sur->sel_obs = 0;
2283         }
2284         bool feedback_on = sur->feedback.to_ulong();
2285         if (s && feedback_on) {
2286                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2287                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2288                 sur->sel_obs = sel_fb;
2289         } else if (sur->expand_enable) {
2290                 sur->expand = 0;
2291                 sur->expand_enable = false;
2292                 if (_select && feedback_on) {
2293                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2294                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2295                         sur->sel_obs = sel_fb;
2296                 }
2297         } else if (feedback_on) {
2298                 route_send_fail ("select", sur->expand, 0 , addr);
2299         }
2300         if (!feedback_on) {
2301                 return 0;
2302         }
2303         //update buttons on surface
2304         int b_s = sur->bank_size;
2305         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2306                 b_s = sur->nstrips;
2307         }
2308         for (int i = 1;  i <= b_s; i++) {
2309                 string path = "expand";
2310
2311                 if ((i == (int) sur->expand) && sur->expand_enable) {
2312                         lo_message reply = lo_message_new ();
2313                         if (sur->feedback[2]) {
2314                                 ostringstream os;
2315                                 os << "/strip/" << path << "/" << i;
2316                                 path = os.str();
2317                         } else {
2318                                 ostringstream os;
2319                                 os << "/strip/" << path;
2320                                 path = os.str();
2321                                 lo_message_add_int32 (reply, i);
2322                         }
2323                         lo_message_add_float (reply, (float) 1);
2324
2325                         lo_send_message (addr, path.c_str(), reply);
2326                         lo_message_free (reply);
2327                         reply = lo_message_new ();
2328                         lo_message_add_float (reply, 1.0);
2329                         lo_send_message (addr, "/select/expand", reply);
2330                         lo_message_free (reply);
2331
2332                 } else {
2333                         lo_message reply = lo_message_new ();
2334                         lo_message_add_int32 (reply, i);
2335                         lo_message_add_float (reply, 0.0);
2336                         lo_send_message (addr, "/strip/expand", reply);
2337                         lo_message_free (reply);
2338                 }
2339         }
2340         if (!sur->expand_enable) {
2341                 lo_message reply = lo_message_new ();
2342                 lo_message_add_float (reply, 0.0);
2343                 lo_send_message (addr, "/select/expand", reply);
2344                 lo_message_free (reply);
2345         }
2346
2347         return 0;
2348 }
2349
2350 int
2351 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2352 {
2353         //ignore button release
2354         if (!yn) return 0;
2355
2356         if (!session) {
2357                 return -1;
2358         }
2359         OSCSurface *sur = get_surface(get_address (msg));
2360         sur->expand_enable = false;
2361         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2362         if (s) {
2363                 SetStripableSelection (s);
2364         } else {
2365                 if ((int) (sur->feedback.to_ulong())) {
2366                         route_send_fail ("select", ssid, 0, get_address (msg));
2367                 }
2368         }
2369
2370         return 0;
2371 }
2372
2373 int
2374 OSC::sel_expand (uint32_t state, lo_message msg)
2375 {
2376         OSCSurface *sur = get_surface(get_address (msg));
2377         boost::shared_ptr<Stripable> s;
2378         sur->expand_enable = (bool) state;
2379         if (state && sur->expand) {
2380                 s = get_strip (sur->expand, get_address (msg));
2381         } else {
2382                 s = ControlProtocol::first_selected_stripable();
2383         }
2384
2385         return _strip_select (s, get_address (msg));
2386 }
2387
2388 int
2389 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2390 {
2391         if (!session) return -1;
2392         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2393
2394         if (s) {
2395                 if (s->gain_control()) {
2396                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2397                 } else {
2398                         return 1;
2399                 }
2400         } else {
2401                 return 1;
2402         }
2403
2404         return 0;
2405 }
2406
2407 int
2408 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2409 {
2410         if (!session) {
2411                 route_send_fail ("gain", ssid, -193, get_address (msg));
2412                 return -1;
2413         }
2414         int ret;
2415         if (dB < -192) {
2416                 ret = route_set_gain_abs (ssid, 0.0, msg);
2417         } else {
2418                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2419         }
2420         if (ret != 0) {
2421                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2422         }
2423         return 0;
2424 }
2425
2426 int
2427 OSC::sel_gain (float val, lo_message msg)
2428 {
2429         OSCSurface *sur = get_surface(get_address (msg));
2430         boost::shared_ptr<Stripable> s;
2431         if (sur->expand_enable) {
2432                 s = get_strip (sur->expand, get_address (msg));
2433         } else {
2434                 s = _select;
2435         }
2436         if (s) {
2437                 float abs;
2438                 if (val < -192) {
2439                         abs = 0;
2440                 } else {
2441                         abs = dB_to_coefficient (val);
2442                 }
2443                 if (s->gain_control()) {
2444                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2445                         return 0;
2446                 }
2447         }
2448         return sel_fail ("gain", -193, get_address (msg));
2449 }
2450
2451 int
2452 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2453 {
2454         if (!session) {
2455                 route_send_fail ("fader", ssid, 0, get_address (msg));
2456                 return -1;
2457         }
2458         int ret;
2459         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2460         if (ret != 0) {
2461                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2462         }
2463         return 0;
2464 }
2465
2466 int
2467 OSC::sel_fader (float val, lo_message msg)
2468 {
2469         OSCSurface *sur = get_surface(get_address (msg));
2470         boost::shared_ptr<Stripable> s;
2471         if (sur->expand_enable) {
2472                 s = get_strip (sur->expand, get_address (msg));
2473         } else {
2474                 s = _select;
2475         }
2476         if (s) {
2477                 float abs;
2478                 abs = slider_position_to_gain_with_max (val, 2.0);
2479                 if (s->gain_control()) {
2480                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2481                         return 0;
2482                 }
2483         }
2484         return sel_fail ("fader", 0, get_address (msg));
2485 }
2486
2487 int
2488 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2489 {
2490         if (!session) return -1;
2491         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2492
2493         if (s) {
2494                 if (s->trim_control()) {
2495                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2496                         return 0;
2497                 }
2498
2499         }
2500
2501         return -1;
2502 }
2503
2504 int
2505 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2506 {
2507         int ret;
2508         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2509         if (ret != 0) {
2510                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2511         }
2512
2513 return 0;
2514 }
2515
2516 int
2517 OSC::sel_trim (float val, lo_message msg)
2518 {
2519         OSCSurface *sur = get_surface(get_address (msg));
2520         boost::shared_ptr<Stripable> s;
2521         if (sur->expand_enable) {
2522                 s = get_strip (sur->expand, get_address (msg));
2523         } else {
2524                 s = _select;
2525         }
2526         if (s) {
2527                 if (s->trim_control()) {
2528                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2529                         return 0;
2530                 }
2531         }
2532         return sel_fail ("trimdB", 0, get_address (msg));
2533 }
2534
2535 int
2536 OSC::sel_pan_position (float val, lo_message msg)
2537 {
2538         OSCSurface *sur = get_surface(get_address (msg));
2539         boost::shared_ptr<Stripable> s;
2540         if (sur->expand_enable) {
2541                 s = get_strip (sur->expand, get_address (msg));
2542         } else {
2543                 s = _select;
2544         }
2545         if (s) {
2546                 if(s->pan_azimuth_control()) {
2547                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2548                         return sel_fail ("pan_stereo_position", s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ()), get_address (msg));
2549                         return 0;
2550                 }
2551         }
2552         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2553 }
2554
2555 int
2556 OSC::sel_pan_width (float val, lo_message msg)
2557 {
2558         OSCSurface *sur = get_surface(get_address (msg));
2559         boost::shared_ptr<Stripable> s;
2560         if (sur->expand_enable) {
2561                 s = get_strip (sur->expand, get_address (msg));
2562         } else {
2563                 s = _select;
2564         }
2565         if (s) {
2566                 if (s->pan_width_control()) {
2567                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2568                         return 0;
2569                 }
2570         }
2571         return sel_fail ("pan_stereo_width", 1, get_address (msg));
2572 }
2573
2574 int
2575 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
2576 {
2577         if (!session) return -1;
2578         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2579
2580         if (s) {
2581                 if(s->pan_azimuth_control()) {
2582                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
2583                         return route_send_fail ("pan_stereo_position", ssid, s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ()), get_address (msg));
2584                 }
2585         }
2586
2587         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
2588 }
2589
2590 int
2591 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
2592 {
2593         if (!session) return -1;
2594         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2595
2596         if (s) {
2597                 if (s->pan_width_control()) {
2598                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
2599                         return 0;
2600                 }
2601         }
2602
2603         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
2604 }
2605
2606 int
2607 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
2608 {
2609         if (!session) {
2610                 return -1;
2611         }
2612         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2613         float abs;
2614         if (s) {
2615                 if (id > 0) {
2616                         --id;
2617                 }
2618 #ifdef MIXBUS
2619                 abs = val;
2620 #else
2621                 if (val < -192) {
2622                         abs = 0;
2623                 } else {
2624                         abs = dB_to_coefficient (val);
2625                 }
2626 #endif
2627                 if (s->send_level_controllable (id)) {
2628                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2629                         return 0;
2630                 }
2631         }
2632         return 0;
2633 }
2634
2635 int
2636 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
2637 {
2638         if (!session) {
2639                 return -1;
2640         }
2641         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2642         float abs;
2643         if (s) {
2644
2645                 if (id > 0) {
2646                         --id;
2647                 }
2648
2649                 if (s->send_level_controllable (id)) {
2650 #ifdef MIXBUS
2651                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2652 #else
2653                         abs = slider_position_to_gain_with_max (val, 2.0);
2654 #endif
2655                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2656                         return 0;
2657                 }
2658         }
2659         return 0;
2660 }
2661
2662 int
2663 OSC::sel_sendgain (int id, float val, lo_message msg)
2664 {
2665         OSCSurface *sur = get_surface(get_address (msg));
2666         boost::shared_ptr<Stripable> s;
2667         if (sur->expand_enable) {
2668                 s = get_strip (sur->expand, get_address (msg));
2669         } else {
2670                 s = _select;
2671         }
2672         float abs;
2673         if (s) {
2674                 if (id > 0) {
2675                         --id;
2676                 }
2677 #ifdef MIXBUS
2678                 abs = val;
2679 #else
2680                 if (val < -192) {
2681                         abs = 0;
2682                 } else {
2683                         abs = dB_to_coefficient (val);
2684                 }
2685 #endif
2686                 if (s->send_level_controllable (id)) {
2687                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2688                         return 0;
2689                 }
2690         }
2691         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
2692 }
2693
2694 int
2695 OSC::sel_sendfader (int id, float val, lo_message msg)
2696 {
2697         OSCSurface *sur = get_surface(get_address (msg));
2698         boost::shared_ptr<Stripable> s;
2699         if (sur->expand_enable) {
2700                 s = get_strip (sur->expand, get_address (msg));
2701         } else {
2702                 s = _select;
2703         }
2704         float abs;
2705         if (s) {
2706
2707                 if (id > 0) {
2708                         --id;
2709                 }
2710
2711                 if (s->send_level_controllable (id)) {
2712 #ifdef MIXBUS
2713                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2714 #else
2715                         abs = slider_position_to_gain_with_max (val, 2.0);
2716 #endif
2717                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2718                         return 0;
2719                 }
2720         }
2721         return sel_send_fail ("send_fader", id, 0, get_address (msg));
2722 }
2723
2724 int
2725 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
2726 {
2727         if (!session) {
2728                 return -1;
2729         }
2730         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2731
2732         if (s) {
2733
2734                 /* revert to zero-based counting */
2735
2736                 if (sid > 0) {
2737                         --sid;
2738                 }
2739
2740                 if (s->send_enable_controllable (sid)) {
2741                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
2742                         return 0;
2743                 }
2744
2745                 if (s->send_level_controllable (sid)) {
2746                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2747                         if (!r) {
2748                                 return 0;
2749                         }
2750                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
2751                         if (snd) {
2752                                 if (val) {
2753                                         snd->activate();
2754                                 } else {
2755                                         snd->deactivate();
2756                                 }
2757                         }
2758                         return 0;
2759                 }
2760
2761         }
2762
2763         return -1;
2764 }
2765
2766 int
2767 OSC::sel_sendenable (int id, float val, lo_message msg)
2768 {
2769         OSCSurface *sur = get_surface(get_address (msg));
2770         boost::shared_ptr<Stripable> s;
2771         if (sur->expand_enable) {
2772                 s = get_strip (sur->expand, get_address (msg));
2773         } else {
2774                 s = _select;
2775         }
2776         if (s) {
2777                 if (id > 0) {
2778                         --id;
2779                 }
2780                 if (s->send_enable_controllable (id)) {
2781                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
2782                         return 0;
2783                 }
2784                 if (s->send_level_controllable (id)) {
2785                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2786                         if (!r) {
2787                                 // should never get here
2788                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
2789                         }
2790                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
2791                         if (snd) {
2792                                 if (val) {
2793                                         snd->activate();
2794                                 } else {
2795                                         snd->deactivate();
2796                                 }
2797                         }
2798                         return 0;
2799                 }
2800         }
2801         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
2802 }
2803
2804 int
2805 OSC::route_plugin_list (int ssid, lo_message msg) {
2806         if (!session) {
2807                 return -1;
2808         }
2809
2810         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
2811
2812         if (!r) {
2813                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2814                 return -1;
2815         }
2816         int piid = 0;
2817
2818         lo_message reply = lo_message_new ();
2819         lo_message_add_int32 (reply, ssid);
2820
2821
2822         for (;;) {
2823                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
2824                 if ( !redi ) {
2825                         break;
2826                 }
2827
2828                 boost::shared_ptr<PluginInsert> pi;
2829
2830                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2831                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2832                         continue;
2833                 }
2834                 lo_message_add_int32 (reply, piid + 1);
2835
2836                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2837                 lo_message_add_string (reply, pip->name());
2838
2839                 piid++;
2840         }
2841
2842         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
2843         lo_message_free (reply);
2844         return 0;
2845 }
2846
2847 int
2848 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
2849         if (!session) {
2850                 return -1;
2851         }
2852
2853         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
2854
2855         if (!r) {
2856                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2857                 return -1;
2858         }
2859
2860         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
2861
2862         if (!redi) {
2863                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2864                 return -1;
2865         }
2866
2867         boost::shared_ptr<PluginInsert> pi;
2868
2869         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2870                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2871                 return -1;
2872         }
2873
2874         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2875         bool ok = false;
2876
2877         lo_message reply = lo_message_new();
2878         lo_message_add_int32 (reply, ssid);
2879         lo_message_add_int32 (reply, piid);
2880         lo_message_add_string (reply, pip->name());
2881         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
2882
2883                 uint32_t controlid = pip->nth_parameter(ppi, ok);
2884                 if (!ok) {
2885                         continue;
2886                 }
2887                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
2888                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
2889
2890                                 lo_message_add_int32 (reply, ppi + 1);
2891                                 ParameterDescriptor pd;
2892                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
2893                                 lo_message_add_string (reply, pd.label.c_str());
2894
2895                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
2896                                 int flags = 0;
2897                                 flags |= pd.enumeration ? 1 : 0;
2898                                 flags |= pd.integer_step ? 2 : 0;
2899                                 flags |= pd.logarithmic ? 4 : 0;
2900                                 flags |= pd.max_unbound ? 8 : 0;
2901                                 flags |= pd.min_unbound ? 16 : 0;
2902                                 flags |= pd.sr_dependent ? 32 : 0;
2903                                 flags |= pd.toggled ? 64 : 0;
2904                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
2905                                 lo_message_add_int32 (reply, flags);
2906
2907                                 lo_message_add_int32 (reply, pd.datatype);
2908                                 lo_message_add_float (reply, pd.lower);
2909                                 lo_message_add_float (reply, pd.upper);
2910                                 lo_message_add_string (reply, pd.print_fmt.c_str());
2911                                 if ( pd.scale_points ) {
2912                                         lo_message_add_int32 (reply, pd.scale_points->size());
2913                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
2914                                                 lo_message_add_int32 (reply, i->second);
2915                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
2916                                         }
2917                                 }
2918                                 else {
2919                                         lo_message_add_int32 (reply, 0);
2920                                 }
2921                                 if ( c ) {
2922                                         lo_message_add_double (reply, c->get_value());
2923                                 }
2924                                 else {
2925                                         lo_message_add_double (reply, 0);
2926                         }
2927                 }
2928         }
2929
2930         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
2931         lo_message_free (reply);
2932
2933         return 0;
2934 }
2935
2936 int
2937 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
2938         if (!session) {
2939                 return -1;
2940         }
2941
2942         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
2943
2944         if (!r) {
2945                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2946                 return -1;
2947         }
2948
2949         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
2950
2951         if (!redi) {
2952                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2953                 return -1;
2954         }
2955
2956         boost::shared_ptr<PluginInsert> pi;
2957
2958         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2959                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2960                 return -1;
2961         }
2962
2963         pi->reset_parameters_to_default ();
2964
2965         return 0;
2966 }
2967
2968 int
2969 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
2970 {
2971         if (!session)
2972                 return -1;
2973         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2974
2975         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
2976
2977         if (!r) {
2978                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
2979                 return -1;
2980         }
2981
2982         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
2983
2984         if (!redi) {
2985                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
2986                 return -1;
2987         }
2988
2989         boost::shared_ptr<PluginInsert> pi;
2990
2991         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
2992                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
2993                 return -1;
2994         }
2995
2996         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2997         bool ok=false;
2998
2999         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3000
3001         if (!ok) {
3002                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3003                 return -1;
3004         }
3005
3006         if (!pip->parameter_is_input(controlid)) {
3007                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3008                 return -1;
3009         }
3010
3011         ParameterDescriptor pd;
3012         pi->plugin()->get_parameter_descriptor (controlid,pd);
3013
3014         if (val >= pd.lower && val <= pd.upper) {
3015
3016                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3017                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3018                 c->set_value (val, PBD::Controllable::NoGroup);
3019         } else {
3020                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3021                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3022         }
3023
3024         return 0;
3025 }
3026
3027 //prints to cerr only
3028 int
3029 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3030 {
3031         if (!session) {
3032                 return -1;
3033         }
3034         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3035
3036         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3037
3038         if (!r) {
3039                 return -1;
3040         }
3041
3042         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3043
3044         if (!redi) {
3045                 return -1;
3046         }
3047
3048         boost::shared_ptr<PluginInsert> pi;
3049
3050         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3051                 return -1;
3052         }
3053
3054         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3055         bool ok=false;
3056
3057         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3058
3059         if (!ok) {
3060                 return -1;
3061         }
3062
3063         ParameterDescriptor pd;
3064
3065         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3066                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3067
3068                 cerr << "parameter:     " << pd.label  << "\n";
3069                 if (c) {
3070                         cerr << "current value: " << c->get_value () << "\n";
3071                 } else {
3072                         cerr << "current value not available, control does not exist\n";
3073                 }
3074                 cerr << "lower value:   " << pd.lower << "\n";
3075                 cerr << "upper value:   " << pd.upper << "\n";
3076         }
3077
3078         return 0;
3079 }
3080
3081 int
3082 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3083 {
3084         if (!session)
3085                 return -1;
3086         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3087
3088         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3089
3090         if (!r) {
3091                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3092                 return -1;
3093         }
3094
3095         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3096
3097         if (!redi) {
3098                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3099                 return -1;
3100         }
3101
3102         boost::shared_ptr<PluginInsert> pi;
3103
3104         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3105                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3106                 return -1;
3107         }
3108
3109         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3110         pi->activate();
3111
3112         return 0;
3113 }
3114
3115 int
3116 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3117 {
3118         if (!session)
3119                 return -1;
3120         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3121
3122         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3123
3124         if (!r) {
3125                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3126                 return -1;
3127         }
3128
3129         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3130
3131         if (!redi) {
3132                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3133                 return -1;
3134         }
3135
3136         boost::shared_ptr<PluginInsert> pi;
3137
3138         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3139                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3140                 return -1;
3141         }
3142
3143         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3144         pi->deactivate();
3145
3146         return 0;
3147 }
3148
3149 // select
3150
3151 int
3152 OSC::sel_pan_elevation (float val, lo_message msg)
3153 {
3154         OSCSurface *sur = get_surface(get_address (msg));
3155         boost::shared_ptr<Stripable> s;
3156         if (sur->expand_enable) {
3157                 s = get_strip (sur->expand, get_address (msg));
3158         } else {
3159                 s = _select;
3160         }
3161         if (s) {
3162                 if (s->pan_elevation_control()) {
3163                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3164                         return 0;
3165                 }
3166         }
3167         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3168 }
3169
3170 int
3171 OSC::sel_pan_frontback (float val, lo_message msg)
3172 {
3173         OSCSurface *sur = get_surface(get_address (msg));
3174         boost::shared_ptr<Stripable> s;
3175         if (sur->expand_enable) {
3176                 s = get_strip (sur->expand, get_address (msg));
3177         } else {
3178                 s = _select;
3179         }
3180         if (s) {
3181                 if (s->pan_frontback_control()) {
3182                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3183                         return 0;
3184                 }
3185         }
3186         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3187 }
3188
3189 int
3190 OSC::sel_pan_lfe (float val, lo_message msg)
3191 {
3192         OSCSurface *sur = get_surface(get_address (msg));
3193         boost::shared_ptr<Stripable> s;
3194         if (sur->expand_enable) {
3195                 s = get_strip (sur->expand, get_address (msg));
3196         } else {
3197                 s = _select;
3198         }
3199         if (s) {
3200                 if (s->pan_lfe_control()) {
3201                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3202                         return 0;
3203                 }
3204         }
3205         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3206 }
3207
3208 // compressor control
3209 int
3210 OSC::sel_comp_enable (float val, lo_message msg)
3211 {
3212         OSCSurface *sur = get_surface(get_address (msg));
3213         boost::shared_ptr<Stripable> s;
3214         if (sur->expand_enable) {
3215                 s = get_strip (sur->expand, get_address (msg));
3216         } else {
3217                 s = _select;
3218         }
3219         if (s) {
3220                 if (s->comp_enable_controllable()) {
3221                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3222                         return 0;
3223                 }
3224         }
3225         return sel_fail ("comp_enable", 0, get_address (msg));
3226 }
3227
3228 int
3229 OSC::sel_comp_threshold (float val, lo_message msg)
3230 {
3231         OSCSurface *sur = get_surface(get_address (msg));
3232         boost::shared_ptr<Stripable> s;
3233         if (sur->expand_enable) {
3234                 s = get_strip (sur->expand, get_address (msg));
3235         } else {
3236                 s = _select;
3237         }
3238         if (s) {
3239                 if (s->comp_threshold_controllable()) {
3240                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3241                         return 0;
3242                 }
3243         }
3244         return sel_fail ("comp_threshold", 0, get_address (msg));
3245 }
3246
3247 int
3248 OSC::sel_comp_speed (float val, lo_message msg)
3249 {
3250         OSCSurface *sur = get_surface(get_address (msg));
3251         boost::shared_ptr<Stripable> s;
3252         if (sur->expand_enable) {
3253                 s = get_strip (sur->expand, get_address (msg));
3254         } else {
3255                 s = _select;
3256         }
3257         if (s) {
3258                 if (s->comp_speed_controllable()) {
3259                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3260                         return 0;
3261                 }
3262         }
3263         return sel_fail ("comp_speed", 0, get_address (msg));
3264 }
3265
3266 int
3267 OSC::sel_comp_mode (float val, lo_message msg)
3268 {
3269         OSCSurface *sur = get_surface(get_address (msg));
3270         boost::shared_ptr<Stripable> s;
3271         if (sur->expand_enable) {
3272                 s = get_strip (sur->expand, get_address (msg));
3273         } else {
3274                 s = _select;
3275         }
3276         if (s) {
3277                 if (s->comp_mode_controllable()) {
3278                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3279                         return 0;
3280                 }
3281         }
3282         return sel_fail ("comp_mode", 0, get_address (msg));
3283 }
3284
3285 int
3286 OSC::sel_comp_makeup (float val, lo_message msg)
3287 {
3288         OSCSurface *sur = get_surface(get_address (msg));
3289         boost::shared_ptr<Stripable> s;
3290         if (sur->expand_enable) {
3291                 s = get_strip (sur->expand, get_address (msg));
3292         } else {
3293                 s = _select;
3294         }
3295         if (s) {
3296                 if (s->comp_makeup_controllable()) {
3297                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3298                         return 0;
3299                 }
3300         }
3301         return sel_fail ("comp_makeup", 0, get_address (msg));
3302 }
3303
3304 // EQ control
3305
3306 int
3307 OSC::sel_eq_enable (float val, lo_message msg)
3308 {
3309         OSCSurface *sur = get_surface(get_address (msg));
3310         boost::shared_ptr<Stripable> s;
3311         if (sur->expand_enable) {
3312                 s = get_strip (sur->expand, get_address (msg));
3313         } else {
3314                 s = _select;
3315         }
3316         if (s) {
3317                 if (s->eq_enable_controllable()) {
3318                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3319                         return 0;
3320                 }
3321         }
3322         return sel_fail ("eq_enable", 0, get_address (msg));
3323 }
3324
3325 int
3326 OSC::sel_eq_hpf (float val, lo_message msg)
3327 {
3328         OSCSurface *sur = get_surface(get_address (msg));
3329         boost::shared_ptr<Stripable> s;
3330         if (sur->expand_enable) {
3331                 s = get_strip (sur->expand, get_address (msg));
3332         } else {
3333                 s = _select;
3334         }
3335         if (s) {
3336                 if (s->eq_hpf_controllable()) {
3337                         s->eq_hpf_controllable()->set_value (s->eq_hpf_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3338                         return 0;
3339                 }
3340         }
3341         return sel_fail ("eq_hpf", 0, get_address (msg));
3342 }
3343
3344 int
3345 OSC::sel_eq_gain (int id, float val, lo_message msg)
3346 {
3347         OSCSurface *sur = get_surface(get_address (msg));
3348         boost::shared_ptr<Stripable> s;
3349         if (sur->expand_enable) {
3350                 s = get_strip (sur->expand, get_address (msg));
3351         } else {
3352                 s = _select;
3353         }
3354         if (s) {
3355                 if (id > 0) {
3356                         --id;
3357                 }
3358                 if (s->eq_gain_controllable (id)) {
3359                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3360                         return 0;
3361                 }
3362         }
3363         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3364 }
3365
3366 int
3367 OSC::sel_eq_freq (int id, float val, lo_message msg)
3368 {
3369         OSCSurface *sur = get_surface(get_address (msg));
3370         boost::shared_ptr<Stripable> s;
3371         if (sur->expand_enable) {
3372                 s = get_strip (sur->expand, get_address (msg));
3373         } else {
3374                 s = _select;
3375         }
3376         if (s) {
3377                 if (id > 0) {
3378                         --id;
3379                 }
3380                 if (s->eq_freq_controllable (id)) {
3381                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3382                         return 0;
3383                 }
3384         }
3385         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3386 }
3387
3388 int
3389 OSC::sel_eq_q (int id, float val, lo_message msg)
3390 {
3391         OSCSurface *sur = get_surface(get_address (msg));
3392         boost::shared_ptr<Stripable> s;
3393         if (sur->expand_enable) {
3394                 s = get_strip (sur->expand, get_address (msg));
3395         } else {
3396                 s = _select;
3397         }
3398         if (s) {
3399                 if (id > 0) {
3400                         --id;
3401                 }
3402                 if (s->eq_q_controllable (id)) {
3403                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3404                         return 0;
3405                 }
3406         }
3407         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3408 }
3409
3410 int
3411 OSC::sel_eq_shape (int id, float val, lo_message msg)
3412 {
3413         OSCSurface *sur = get_surface(get_address (msg));
3414         boost::shared_ptr<Stripable> s;
3415         if (sur->expand_enable) {
3416                 s = get_strip (sur->expand, get_address (msg));
3417         } else {
3418                 s = _select;
3419         }
3420         if (s) {
3421                 if (id > 0) {
3422                         --id;
3423                 }
3424                 if (s->eq_shape_controllable (id)) {
3425                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3426                         return 0;
3427                 }
3428         }
3429         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3430 }
3431
3432 void
3433 OSC::gui_selection_changed ()
3434 {
3435         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3436
3437         if (strip) {
3438                 _select = strip;
3439                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3440                         OSCSurface* sur = &_surface[it];
3441                         if(!sur->expand_enable) {
3442                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3443                                 _strip_select (strip, addr);
3444                         }
3445                 }
3446         }
3447 }
3448
3449 // timer callbacks
3450 bool
3451 OSC::periodic (void)
3452 {
3453         if (!tick) {
3454                 Glib::usleep(100); // let flurry of signals subside
3455                 if (global_init) {
3456                         for (uint32_t it = 0; it < _surface.size(); it++) {
3457                                 OSCSurface* sur = &_surface[it];
3458                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3459                                 global_feedback (sur->feedback, addr, sur->gainmode);
3460                         }
3461                         global_init = false;
3462                         tick = true;
3463                 }
3464                 if (bank_dirty) {
3465                         _recalcbanks ();
3466                         bank_dirty = false;
3467                         tick = true;
3468                 }
3469         }
3470
3471         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3472
3473                 OSCGlobalObserver* go;
3474
3475                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3476                         go->tick();
3477                 }
3478         }
3479         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3480
3481                 OSCRouteObserver* ro;
3482
3483                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3484                         ro->tick();
3485                 }
3486         }
3487         for (uint32_t it = 0; it < _surface.size(); it++) {
3488                 OSCSurface* sur = &_surface[it];
3489                 OSCSelectObserver* so;
3490                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3491                         so->tick();
3492                 }
3493         }
3494         return true;
3495 }
3496
3497 int
3498 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3499 {
3500         OSCSurface *sur = get_surface(addr);
3501
3502         ostringstream os;
3503         lo_message reply;
3504         if (ssid) {
3505                 reply = lo_message_new ();
3506                 if (sur->feedback[2]) {
3507                         os << "/strip/" << path << "/" << ssid;
3508                 } else {
3509                         os << "/strip/" << path;
3510                         lo_message_add_int32 (reply, ssid);
3511                 }
3512                 string str_pth = os.str();
3513                 lo_message_add_float (reply, (float) val);
3514
3515                 lo_send_message (addr, str_pth.c_str(), reply);
3516                 lo_message_free (reply);
3517         }
3518         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3519                 os.str("");
3520                 os << "/select/" << path;
3521                 string sel_pth = os.str();
3522                 reply = lo_message_new ();
3523                 lo_message_add_float (reply, (float) val);
3524                 lo_send_message (addr, sel_pth.c_str(), reply);
3525                 lo_message_free (reply);
3526         }
3527
3528         return 0;
3529 }
3530
3531 int
3532 OSC::sel_fail (string path, float val, lo_address addr)
3533 {
3534         ostringstream os;
3535         os.str("");
3536         os << "/select/" << path;
3537         string sel_pth = os.str();
3538         lo_message reply = lo_message_new ();
3539         lo_message_add_float (reply, (float) val);
3540         lo_send_message (addr, sel_pth.c_str(), reply);
3541         lo_message_free (reply);
3542
3543         return 0;
3544 }
3545
3546 int
3547 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
3548 {
3549         OSCSurface *sur = get_surface(addr);
3550
3551         ostringstream os;
3552         lo_message reply;
3553         reply = lo_message_new ();
3554         if (sur->feedback[2]) {
3555                 os << "/select/" << path << "/" << id;
3556         } else {
3557                 os << "/select/" << path;
3558                 lo_message_add_int32 (reply, id);
3559         }
3560         string str_pth = os.str();
3561         lo_message_add_float (reply, (float) val);
3562
3563         lo_send_message (addr, str_pth.c_str(), reply);
3564         lo_message_free (reply);
3565
3566         return 0;
3567 }
3568
3569 XMLNode&
3570 OSC::get_state ()
3571 {
3572         XMLNode& node (ControlProtocol::get_state());
3573         node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
3574         node.add_property ("address-only", address_only);
3575         node.add_property ("remote-port", remote_port);
3576         node.add_property ("banksize", default_banksize);
3577         node.add_property ("striptypes", default_strip);
3578         node.add_property ("feedback", default_feedback);
3579         node.add_property ("gainmode", default_gainmode);
3580         if (_surface.size()) {
3581                 XMLNode* config = new XMLNode (X_("Configurations"));
3582                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3583                         OSCSurface* sur = &_surface[it];
3584                         XMLNode* devnode = new XMLNode (X_("Configuration"));
3585                         devnode->add_property (X_("url"), sur->remote_url);
3586                         devnode->add_property (X_("bank-size"), sur->bank_size);
3587                         devnode->add_property (X_("strip-types"), sur->strip_types.to_ulong());
3588                         devnode->add_property (X_("feedback"), sur->feedback.to_ulong());
3589                         devnode->add_property (X_("gainmode"), sur->gainmode);
3590                         config->add_child_nocopy (*devnode);
3591                 }
3592                 node.add_child_nocopy (*config);
3593         }
3594         return node;
3595 }
3596
3597 int
3598 OSC::set_state (const XMLNode& node, int version)
3599 {
3600         if (ControlProtocol::set_state (node, version)) {
3601                 return -1;
3602         }
3603         XMLProperty const * p = node.property (X_("debugmode"));
3604         if (p) {
3605                 _debugmode = OSCDebugMode (PBD::atoi(p->value ()));
3606         }
3607         p = node.property (X_("address-only"));
3608         if (p) {
3609                 address_only = OSCDebugMode (PBD::atoi(p->value ()));
3610         }
3611         p = node.property (X_("remote-port"));
3612         if (p) {
3613                 remote_port = p->value ();
3614         }
3615         p = node.property (X_("banksize"));
3616         if (p) {
3617                 default_banksize = OSCDebugMode (PBD::atoi(p->value ()));
3618         }
3619         p = node.property (X_("striptypes"));
3620         if (p) {
3621                 default_strip = OSCDebugMode (PBD::atoi(p->value ()));
3622         }
3623         p = node.property (X_("feedback"));
3624         if (p) {
3625                 default_feedback = OSCDebugMode (PBD::atoi(p->value ()));
3626         }
3627         p = node.property (X_("gainmode"));
3628         if (p) {
3629                 default_gainmode = OSCDebugMode (PBD::atoi(p->value ()));
3630         }
3631         XMLNode* cnode = node.child (X_("Configurations"));
3632
3633         if (cnode) {
3634                 XMLNodeList const& devices = cnode->children();
3635                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
3636                         XMLProperty const * prop = (*d)->property (X_("url"));
3637                         if (prop) {
3638                                 OSCSurface s;
3639                                 bank_dirty = true;
3640                                 s.remote_url = prop->value();
3641                                 prop = (*d)->property (X_("bank-size"));
3642                                 if (prop) {
3643                                         s.bank_size = atoi (prop->value().c_str());
3644                                 }
3645                                 prop = (*d)->property (X_("strip-types"));
3646                                 if (prop) {
3647                                         s.strip_types = atoi (prop->value().c_str());
3648                                 }
3649                                 prop = (*d)->property (X_("feedback"));
3650                                 if (prop) {
3651                                         s.feedback = atoi (prop->value().c_str());
3652                                 }
3653                                 prop = (*d)->property (X_("gainmode"));
3654                                 if (prop) {
3655                                         s.gainmode = atoi (prop->value().c_str());
3656                                 }
3657                                 s.bank = 1;
3658                                 s.sel_obs = 0;
3659                                 s.expand = 0;
3660                                 s.expand_enable = false;
3661                                 s.strips = get_sorted_stripables(s.strip_types);
3662                                 s.nstrips = s.strips.size();
3663                                 _surface.push_back (s);
3664                         }
3665                 }
3666         }
3667         global_init = true;
3668         tick = false;
3669
3670         return 0;
3671 }
3672
3673 // predicate for sort call in get_sorted_stripables
3674 struct StripableByPresentationOrder
3675 {
3676         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
3677         {
3678                 return a->presentation_info().order() < b->presentation_info().order();
3679         }
3680
3681         bool operator () (const Stripable & a, const Stripable & b) const
3682         {
3683                 return a.presentation_info().order() < b.presentation_info().order();
3684         }
3685
3686         bool operator () (const Stripable * a, const Stripable * b) const
3687         {
3688                 return a->presentation_info().order() < b->presentation_info().order();
3689         }
3690 };
3691
3692 OSC::Sorted
3693 OSC::get_sorted_stripables(std::bitset<32> types)
3694 {
3695         Sorted sorted;
3696
3697         // fetch all stripables
3698         StripableList stripables;
3699
3700         session->get_stripables (stripables);
3701
3702         // Look for stripables that match bit in sur->strip_types
3703         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
3704
3705                 boost::shared_ptr<Stripable> s = *it;
3706                 if ((!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3707                         // do nothing... skip it
3708                 } else {
3709
3710                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
3711                                 sorted.push_back (s);
3712                         } else
3713                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
3714                                 sorted.push_back (s);
3715                         } else
3716                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
3717                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3718                                 // r->feeds (session->master_out()) may make more sense
3719                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
3720                                         // this is a bus
3721                                         if (types[2]) {
3722                                                 sorted.push_back (s);
3723                                         }
3724                                 } else {
3725                                         // this is an Aux out
3726                                         if (types[7]) {
3727                                                 sorted.push_back (s);
3728                                         }
3729                                 }
3730                         } else
3731                         if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
3732                                 sorted.push_back (s);
3733                         } else
3734                         if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
3735                                 sorted.push_back (s);
3736                         } else
3737                         if (types[8] && (s->presentation_info().flags() & PresentationInfo::Selected)) {
3738                                 sorted.push_back (s);
3739                         } else
3740                         if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
3741                                 sorted.push_back (s);
3742                         }
3743                 }
3744         }
3745         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
3746         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
3747         if (types[5]) {
3748                 sorted.push_back (session->master_out());
3749         }
3750         if (types[6]) {
3751                 sorted.push_back (session->monitor_out());
3752         }
3753         return sorted;
3754 }
3755