OSC: simplify GUI and make less error-prone
[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/control_math.h"
32 #include <pbd/convert.h>
33 #include <pbd/pthread_utils.h>
34 #include <pbd/file_utils.h>
35 #include <pbd/failed_constructor.h>
36
37 #include "ardour/amp.h"
38 #include "ardour/session.h"
39 #include "ardour/route.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/midi_track.h"
42 #include "ardour/monitor_control.h"
43 #include "ardour/dB.h"
44 #include "ardour/filesystem_paths.h"
45 #include "ardour/panner.h"
46 #include "ardour/plugin.h"
47 #include "ardour/plugin_insert.h"
48 #include "ardour/presentation_info.h"
49 #include "ardour/profile.h"
50 #include "ardour/send.h"
51 #include "ardour/internal_send.h"
52 #include "ardour/phase_control.h"
53 #include "ardour/solo_isolate_control.h"
54 #include "ardour/solo_safe_control.h"
55 #include "ardour/vca_manager.h"
56
57 #include "osc_select_observer.h"
58 #include "osc.h"
59 #include "osc_controllable.h"
60 #include "osc_route_observer.h"
61 #include "osc_global_observer.h"
62 #include "osc_cue_observer.h"
63 #include "pbd/i18n.h"
64
65 using namespace ARDOUR;
66 using namespace std;
67 using namespace Glib;
68 using namespace ArdourSurface;
69
70 #include "pbd/abstract_ui.cc" // instantiate template
71
72 OSC* OSC::_instance = 0;
73
74 #ifdef DEBUG
75 static void error_callback(int num, const char *m, const char *path)
76 {
77         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
78 }
79 #else
80 static void error_callback(int, const char *, const char *)
81 {
82
83 }
84 #endif
85
86 OSC::OSC (Session& s, uint32_t port)
87         : ControlProtocol (s, X_("Open Sound Control (OSC)"))
88         , AbstractUI<OSCUIRequest> (name())
89         , local_server (0)
90         , remote_server (0)
91         , _port(port)
92         , _ok (true)
93         , _shutdown (false)
94         , _osc_server (0)
95         , _osc_unix_server (0)
96         , _debugmode (Off)
97         , address_only (false)
98         , remote_port ("8000")
99         , default_banksize (0)
100         , default_strip (159)
101         , default_feedback (0)
102         , default_gainmode (0)
103         , default_send_size (0)
104         , default_plugin_size (0)
105         , tick (true)
106         , bank_dirty (false)
107         , scrub_speed (0)
108         , gui (0)
109 {
110         _instance = this;
111
112         session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
113 }
114
115 OSC::~OSC()
116 {
117         stop ();
118         tear_down_gui ();
119         _instance = 0;
120 }
121
122 void*
123 OSC::request_factory (uint32_t num_requests)
124 {
125         /* AbstractUI<T>::request_buffer_factory() is a template method only
126            instantiated in this source module. To provide something visible for
127            use in the interface/descriptor, we have this static method that is
128            template-free.
129         */
130         return request_buffer_factory (num_requests);
131 }
132
133 void
134 OSC::do_request (OSCUIRequest* req)
135 {
136         if (req->type == CallSlot) {
137
138                 call_slot (MISSING_INVALIDATOR, req->the_slot);
139
140         } else if (req->type == Quit) {
141
142                 stop ();
143         }
144 }
145
146 int
147 OSC::set_active (bool yn)
148 {
149         if (yn != active()) {
150
151                 if (yn) {
152                         if (start ()) {
153                                 return -1;
154                         }
155                 } else {
156                         if (stop ()) {
157                                 return -1;
158                         }
159                 }
160
161         }
162
163         return ControlProtocol::set_active (yn);
164 }
165
166 bool
167 OSC::get_active () const
168 {
169         return _osc_server != 0;
170 }
171
172 int
173 OSC::start ()
174 {
175         char tmpstr[255];
176
177         if (_osc_server) {
178                 /* already started */
179                 return 0;
180         }
181
182         for (int j=0; j < 20; ++j) {
183                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
184
185                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
186                 //      break;
187                 //}
188
189                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
190                         break;
191                 }
192
193 #ifdef DEBUG
194                 cerr << "can't get osc at port: " << _port << endl;
195 #endif
196                 _port++;
197                 continue;
198         }
199
200         if (!_osc_server) {
201                 return 1;
202         }
203
204 #ifdef ARDOUR_OSC_UNIX_SERVER
205
206         // APPEARS sluggish for now
207
208         // attempt to create unix socket server too
209
210         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
211         int fd = mkstemp(tmpstr);
212
213         if (fd >= 0 ) {
214                 ::g_unlink (tmpstr);
215                 close (fd);
216
217                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
218
219                 if (_osc_unix_server) {
220                         _osc_unix_socket_path = tmpstr;
221                 }
222         }
223 #endif
224
225         PBD::info << "OSC @ " << get_server_url () << endmsg;
226
227         std::string url_file;
228
229         if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
230                 _osc_url_file = url_file;
231                 if (g_file_set_contents (_osc_url_file.c_str(), get_server_url().c_str(), -1, NULL)) {
232                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
233                 }
234         }
235
236         register_callbacks();
237
238         session_loaded (*session);
239
240         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
241
242         /* startup the event loop thread */
243
244         BaseUI::run ();
245
246         // start timers for metering, timecode and heartbeat.
247         // timecode and metering run at 100
248         Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
249         periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &OSC::periodic));
250         periodic_timeout->attach (main_loop()->get_context());
251
252         // catch track reordering
253         // receive routes added
254         session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_routes_added, this, _1), this);
255         // receive VCAs added
256         session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_vca_added, this, _1), this);
257         // order changed
258         PresentationInfo::Change.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
259
260         _select = boost::shared_ptr<Stripable>();
261
262         return 0;
263 }
264
265 void
266 OSC::thread_init ()
267 {
268         pthread_set_name (event_loop_name().c_str());
269
270         if (_osc_unix_server) {
271                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
272                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
273                 src->attach (_main_loop->get_context());
274                 local_server = src->gobj();
275                 g_source_ref (local_server);
276         }
277
278         if (_osc_server) {
279 #ifdef PLATFORM_WINDOWS
280                 Glib::RefPtr<IOChannel> chan = Glib::IOChannel::create_from_win32_socket (lo_server_get_socket_fd (_osc_server));
281                 Glib::RefPtr<IOSource> src  = IOSource::create (chan, IO_IN|IO_HUP|IO_ERR);
282 #else
283                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
284 #endif
285                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
286                 src->attach (_main_loop->get_context());
287                 remote_server = src->gobj();
288                 g_source_ref (remote_server);
289         }
290
291         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
292         SessionEvent::create_per_thread_pool (event_loop_name(), 128);
293 }
294
295 int
296 OSC::stop ()
297 {
298         /* stop main loop */
299
300         if (local_server) {
301                 g_source_destroy (local_server);
302                 g_source_unref (local_server);
303                 local_server = 0;
304         }
305
306         if (remote_server) {
307                 g_source_destroy (remote_server);
308                 g_source_unref (remote_server);
309                 remote_server = 0;
310         }
311
312         BaseUI::quit ();
313
314         if (_osc_server) {
315                 lo_server_free (_osc_server);
316                 _osc_server = 0;
317         }
318
319         if (_osc_unix_server) {
320                 lo_server_free (_osc_unix_server);
321                 _osc_unix_server = 0;
322         }
323
324         if (!_osc_unix_socket_path.empty()) {
325                 ::g_unlink (_osc_unix_socket_path.c_str());
326         }
327
328         if (!_osc_url_file.empty() ) {
329                 ::g_unlink (_osc_url_file.c_str() );
330         }
331
332         periodic_connection.disconnect ();
333         session_connections.drop_connections ();
334         cueobserver_connections.drop_connections ();
335         // Delete any active route observers
336         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
337
338                 OSCRouteObserver* rc;
339
340                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
341                         delete *x;
342                         x = route_observers.erase (x);
343                 } else {
344                         ++x;
345                 }
346         }
347 // Should maybe do global_observers too
348         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
349
350                 OSCGlobalObserver* gc;
351
352                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
353                         delete *x;
354                         x = global_observers.erase (x);
355                 } else {
356                         ++x;
357                 }
358         }
359
360 // delete select observers
361         for (uint32_t it = 0; it < _surface.size(); ++it) {
362                 OSCSurface* sur = &_surface[it];
363                 OSCSelectObserver* so;
364                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
365                         delete so;
366                 }
367         }
368
369 // delete cue observers
370         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
371
372                 OSCCueObserver* co;
373
374                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
375                         delete *x;
376                         x = cue_observers.erase (x);
377                 } else {
378                         ++x;
379                 }
380         }
381
382         return 0;
383 }
384
385 void
386 OSC::register_callbacks()
387 {
388         lo_server srvs[2];
389         lo_server serv;
390
391         srvs[0] = _osc_server;
392         srvs[1] = _osc_unix_server;
393
394         for (size_t i = 0; i < 2; ++i) {
395
396                 if (!srvs[i]) {
397                         continue;
398                 }
399
400                 serv = srvs[i];
401
402
403 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
404
405                 // Some controls have optional "f" for feedback or touchosc
406                 // http://hexler.net/docs/touchosc-controls-reference
407
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, "/strip/list", "f", routes_list);
412                 REGISTER_CALLBACK (serv, "/add_marker", "", add_marker);
413                 REGISTER_CALLBACK (serv, "/add_marker", "f", add_marker);
414                 REGISTER_CALLBACK (serv, "/access_action", "s", access_action);
415                 REGISTER_CALLBACK (serv, "/loop_toggle", "", loop_toggle);
416                 REGISTER_CALLBACK (serv, "/loop_toggle", "f", loop_toggle);
417                 REGISTER_CALLBACK (serv, "/loop_location", "ii", loop_location);
418                 REGISTER_CALLBACK (serv, "/goto_start", "", goto_start);
419                 REGISTER_CALLBACK (serv, "/goto_start", "f", goto_start);
420                 REGISTER_CALLBACK (serv, "/goto_end", "", goto_end);
421                 REGISTER_CALLBACK (serv, "/goto_end", "f", goto_end);
422                 REGISTER_CALLBACK (serv, "/scrub", "f", scrub);
423                 REGISTER_CALLBACK (serv, "/jog", "f", jog);
424                 REGISTER_CALLBACK (serv, "/jog/mode", "f", jog_mode);
425                 REGISTER_CALLBACK (serv, "/rewind", "", rewind);
426                 REGISTER_CALLBACK (serv, "/rewind", "f", rewind);
427                 REGISTER_CALLBACK (serv, "/ffwd", "", ffwd);
428                 REGISTER_CALLBACK (serv, "/ffwd", "f", ffwd);
429                 REGISTER_CALLBACK (serv, "/transport_stop", "", transport_stop);
430                 REGISTER_CALLBACK (serv, "/transport_stop", "f", transport_stop);
431                 REGISTER_CALLBACK (serv, "/transport_play", "", transport_play);
432                 REGISTER_CALLBACK (serv, "/transport_play", "f", transport_play);
433                 REGISTER_CALLBACK (serv, "/transport_frame", "", transport_frame);
434                 REGISTER_CALLBACK (serv, "/transport_speed", "", transport_speed);
435                 REGISTER_CALLBACK (serv, "/record_enabled", "", record_enabled);
436                 REGISTER_CALLBACK (serv, "/set_transport_speed", "f", set_transport_speed);
437                 // locate ii is position and bool roll
438                 REGISTER_CALLBACK (serv, "/locate", "ii", locate);
439                 REGISTER_CALLBACK (serv, "/save_state", "", save_state);
440                 REGISTER_CALLBACK (serv, "/save_state", "f", save_state);
441                 REGISTER_CALLBACK (serv, "/prev_marker", "", prev_marker);
442                 REGISTER_CALLBACK (serv, "/prev_marker", "f", prev_marker);
443                 REGISTER_CALLBACK (serv, "/next_marker", "", next_marker);
444                 REGISTER_CALLBACK (serv, "/next_marker", "f", next_marker);
445                 REGISTER_CALLBACK (serv, "/undo", "", undo);
446                 REGISTER_CALLBACK (serv, "/undo", "f", undo);
447                 REGISTER_CALLBACK (serv, "/redo", "", redo);
448                 REGISTER_CALLBACK (serv, "/redo", "f", redo);
449                 REGISTER_CALLBACK (serv, "/toggle_punch_in", "", toggle_punch_in);
450                 REGISTER_CALLBACK (serv, "/toggle_punch_in", "f", toggle_punch_in);
451                 REGISTER_CALLBACK (serv, "/toggle_punch_out", "", toggle_punch_out);
452                 REGISTER_CALLBACK (serv, "/toggle_punch_out", "f", toggle_punch_out);
453                 REGISTER_CALLBACK (serv, "/rec_enable_toggle", "", rec_enable_toggle);
454                 REGISTER_CALLBACK (serv, "/rec_enable_toggle", "f", rec_enable_toggle);
455                 REGISTER_CALLBACK (serv, "/toggle_all_rec_enables", "", toggle_all_rec_enables);
456                 REGISTER_CALLBACK (serv, "/toggle_all_rec_enables", "f", toggle_all_rec_enables);
457                 REGISTER_CALLBACK (serv, "/all_tracks_rec_in", "f", all_tracks_rec_in);
458                 REGISTER_CALLBACK (serv, "/all_tracks_rec_out", "f", all_tracks_rec_out);
459                 REGISTER_CALLBACK (serv, "/cancel_all_solos", "f", cancel_all_solos);
460                 REGISTER_CALLBACK (serv, "/remove_marker", "", remove_marker_at_playhead);
461                 REGISTER_CALLBACK (serv, "/remove_marker", "f", remove_marker_at_playhead);
462                 REGISTER_CALLBACK (serv, "/jump_bars", "f", jump_by_bars);
463                 REGISTER_CALLBACK (serv, "/jump_seconds", "f", jump_by_seconds);
464                 REGISTER_CALLBACK (serv, "/mark_in", "", mark_in);
465                 REGISTER_CALLBACK (serv, "/mark_in", "f", mark_in);
466                 REGISTER_CALLBACK (serv, "/mark_out", "", mark_out);
467                 REGISTER_CALLBACK (serv, "/mark_out", "f", mark_out);
468                 REGISTER_CALLBACK (serv, "/toggle_click", "", toggle_click);
469                 REGISTER_CALLBACK (serv, "/toggle_click", "f", toggle_click);
470                 REGISTER_CALLBACK (serv, "/midi_panic", "", midi_panic);
471                 REGISTER_CALLBACK (serv, "/midi_panic", "f", midi_panic);
472                 REGISTER_CALLBACK (serv, "/toggle_roll", "", toggle_roll);
473                 REGISTER_CALLBACK (serv, "/toggle_roll", "f", toggle_roll);
474                 REGISTER_CALLBACK (serv, "/stop_forget", "", stop_forget);
475                 REGISTER_CALLBACK (serv, "/stop_forget", "f", stop_forget);
476                 REGISTER_CALLBACK (serv, "/set_punch_range", "", set_punch_range);
477                 REGISTER_CALLBACK (serv, "/set_punch_range", "f", set_punch_range);
478                 REGISTER_CALLBACK (serv, "/set_loop_range", "", set_loop_range);
479                 REGISTER_CALLBACK (serv, "/set_loop_range", "f", set_loop_range);
480                 REGISTER_CALLBACK (serv, "/set_session_range", "", set_session_range);
481                 REGISTER_CALLBACK (serv, "/set_session_range", "f", set_session_range);
482                 REGISTER_CALLBACK (serv, "/toggle_monitor_mute", "", toggle_monitor_mute);
483                 REGISTER_CALLBACK (serv, "/toggle_monitor_mute", "f", toggle_monitor_mute);
484                 REGISTER_CALLBACK (serv, "/toggle_monitor_dim", "", toggle_monitor_dim);
485                 REGISTER_CALLBACK (serv, "/toggle_monitor_dim", "f", toggle_monitor_dim);
486                 REGISTER_CALLBACK (serv, "/toggle_monitor_mono", "", toggle_monitor_mono);
487                 REGISTER_CALLBACK (serv, "/toggle_monitor_mono", "f", toggle_monitor_mono);
488                 REGISTER_CALLBACK (serv, "/quick_snapshot_switch", "", quick_snapshot_switch);
489                 REGISTER_CALLBACK (serv, "/quick_snapshot_switch", "f", quick_snapshot_switch);
490                 REGISTER_CALLBACK (serv, "/quick_snapshot_stay", "", quick_snapshot_stay);
491                 REGISTER_CALLBACK (serv, "/quick_snapshot_stay", "f", quick_snapshot_stay);
492                 REGISTER_CALLBACK (serv, "/fit_1_track", "", fit_1_track);
493                 REGISTER_CALLBACK (serv, "/fit_1_track", "f", fit_1_track);
494                 REGISTER_CALLBACK (serv, "/fit_2_tracks", "", fit_2_tracks);
495                 REGISTER_CALLBACK (serv, "/fit_2_tracks", "f", fit_2_tracks);
496                 REGISTER_CALLBACK (serv, "/fit_4_tracks", "", fit_4_tracks);
497                 REGISTER_CALLBACK (serv, "/fit_4_tracks", "f", fit_4_tracks);
498                 REGISTER_CALLBACK (serv, "/fit_8_tracks", "", fit_8_tracks);
499                 REGISTER_CALLBACK (serv, "/fit_8_tracks", "f", fit_8_tracks);
500                 REGISTER_CALLBACK (serv, "/fit_16_tracks", "", fit_16_tracks);
501                 REGISTER_CALLBACK (serv, "/fit_16_tracks", "f", fit_16_tracks);
502                 REGISTER_CALLBACK (serv, "/fit_32_tracks", "", fit_32_tracks);
503                 REGISTER_CALLBACK (serv, "/fit_32_tracks", "f", fit_32_tracks);
504                 REGISTER_CALLBACK (serv, "/fit_all_tracks", "", fit_all_tracks);
505                 REGISTER_CALLBACK (serv, "/fit_all_tracks", "f", fit_all_tracks);
506                 REGISTER_CALLBACK (serv, "/zoom_100_ms", "", zoom_100_ms);
507                 REGISTER_CALLBACK (serv, "/zoom_100_ms", "f", zoom_100_ms);
508                 REGISTER_CALLBACK (serv, "/zoom_1_sec", "", zoom_1_sec);
509                 REGISTER_CALLBACK (serv, "/zoom_1_sec", "f", zoom_1_sec);
510                 REGISTER_CALLBACK (serv, "/zoom_10_sec", "", zoom_10_sec);
511                 REGISTER_CALLBACK (serv, "/zoom_10_sec", "f", zoom_10_sec);
512                 REGISTER_CALLBACK (serv, "/zoom_1_min", "", zoom_1_min);
513                 REGISTER_CALLBACK (serv, "/zoom_1_min", "f", zoom_1_min);
514                 REGISTER_CALLBACK (serv, "/zoom_5_min", "", zoom_5_min);
515                 REGISTER_CALLBACK (serv, "/zoom_5_min", "f", zoom_5_min);
516                 REGISTER_CALLBACK (serv, "/zoom_10_min", "", zoom_10_min);
517                 REGISTER_CALLBACK (serv, "/zoom_10_min", "f", zoom_10_min);
518                 REGISTER_CALLBACK (serv, "/zoom_to_session", "", zoom_to_session);
519                 REGISTER_CALLBACK (serv, "/zoom_to_session", "f", zoom_to_session);
520                 REGISTER_CALLBACK (serv, "/temporal_zoom_in", "f", temporal_zoom_in);
521                 REGISTER_CALLBACK (serv, "/temporal_zoom_in", "", temporal_zoom_in);
522                 REGISTER_CALLBACK (serv, "/temporal_zoom_out", "", temporal_zoom_out);
523                 REGISTER_CALLBACK (serv, "/temporal_zoom_out", "f", temporal_zoom_out);
524                 REGISTER_CALLBACK (serv, "/scroll_up_1_track", "f", scroll_up_1_track);
525                 REGISTER_CALLBACK (serv, "/scroll_up_1_track", "", scroll_up_1_track);
526                 REGISTER_CALLBACK (serv, "/scroll_dn_1_track", "f", scroll_dn_1_track);
527                 REGISTER_CALLBACK (serv, "/scroll_dn_1_track", "", scroll_dn_1_track);
528                 REGISTER_CALLBACK (serv, "/scroll_up_1_page", "f", scroll_up_1_page);
529                 REGISTER_CALLBACK (serv, "/scroll_up_1_page", "", scroll_up_1_page);
530                 REGISTER_CALLBACK (serv, "/scroll_dn_1_page", "f", scroll_dn_1_page);
531                 REGISTER_CALLBACK (serv, "/scroll_dn_1_page", "", scroll_dn_1_page);
532                 REGISTER_CALLBACK (serv, "/bank_up", "", bank_up);
533                 REGISTER_CALLBACK (serv, "/bank_up", "f", bank_delta);
534                 REGISTER_CALLBACK (serv, "/bank_down", "", bank_down);
535                 REGISTER_CALLBACK (serv, "/bank_down", "f", bank_down);
536
537                 // controls for "special" strips
538                 REGISTER_CALLBACK (serv, "/master/gain", "f", master_set_gain);
539                 REGISTER_CALLBACK (serv, "/master/fader", "f", master_set_fader);
540                 REGISTER_CALLBACK (serv, "/master/mute", "i", master_set_mute);
541                 REGISTER_CALLBACK (serv, "/master/trimdB", "f", master_set_trim);
542                 REGISTER_CALLBACK (serv, "/master/pan_stereo_position", "f", master_set_pan_stereo_position);
543                 REGISTER_CALLBACK (serv, "/monitor/gain", "f", monitor_set_gain);
544                 REGISTER_CALLBACK (serv, "/monitor/fader", "f", monitor_set_fader);
545                 REGISTER_CALLBACK (serv, "/monitor/mute", "i", monitor_set_mute);
546                 REGISTER_CALLBACK (serv, "/monitor/dim", "i", monitor_set_dim);
547                 REGISTER_CALLBACK (serv, "/monitor/mono", "i", monitor_set_mono);
548
549                 // Controls for the Selected strip
550                 REGISTER_CALLBACK (serv, "/select/recenable", "i", sel_recenable);
551                 REGISTER_CALLBACK (serv, "/select/record_safe", "i", sel_recsafe);
552                 REGISTER_CALLBACK (serv, "/select/mute", "i", sel_mute);
553                 REGISTER_CALLBACK (serv, "/select/solo", "i", sel_solo);
554                 REGISTER_CALLBACK (serv, "/select/solo_iso", "i", sel_solo_iso);
555                 REGISTER_CALLBACK (serv, "/select/solo_safe", "i", sel_solo_safe);
556                 REGISTER_CALLBACK (serv, "/select/monitor_input", "i", sel_monitor_input);
557                 REGISTER_CALLBACK (serv, "/select/monitor_disk", "i", sel_monitor_disk);
558                 REGISTER_CALLBACK (serv, "/select/polarity", "i", sel_phase);
559                 REGISTER_CALLBACK (serv, "/select/gain", "f", sel_gain);
560                 REGISTER_CALLBACK (serv, "/select/fader", "f", sel_fader);
561                 REGISTER_CALLBACK (serv, "/select/trimdB", "f", sel_trim);
562                 REGISTER_CALLBACK (serv, "/select/pan_stereo_position", "f", sel_pan_position);
563                 REGISTER_CALLBACK (serv, "/select/pan_stereo_width", "f", sel_pan_width);
564                 REGISTER_CALLBACK (serv, "/select/send_gain", "if", sel_sendgain);
565                 REGISTER_CALLBACK (serv, "/select/send_fader", "if", sel_sendfader);
566                 REGISTER_CALLBACK (serv, "/select/send_enable", "if", sel_sendenable);
567                 REGISTER_CALLBACK (serv, "/select/master_send_enable", "i", sel_master_send_enable);
568                 REGISTER_CALLBACK (serv, "/select/send_page", "f", sel_send_page);
569                 REGISTER_CALLBACK (serv, "/select/plug_page", "f", sel_plug_page);
570                 REGISTER_CALLBACK (serv, "/select/plugin", "f", sel_plugin);
571                 REGISTER_CALLBACK (serv, "/select/expand", "i", sel_expand);
572                 REGISTER_CALLBACK (serv, "/select/pan_elevation_position", "f", sel_pan_elevation);
573                 REGISTER_CALLBACK (serv, "/select/pan_frontback_position", "f", sel_pan_frontback);
574                 REGISTER_CALLBACK (serv, "/select/pan_lfe_control", "f", sel_pan_lfe);
575                 REGISTER_CALLBACK (serv, "/select/comp_enable", "f", sel_comp_enable);
576                 REGISTER_CALLBACK (serv, "/select/comp_threshold", "f", sel_comp_threshold);
577                 REGISTER_CALLBACK (serv, "/select/comp_speed", "f", sel_comp_speed);
578                 REGISTER_CALLBACK (serv, "/select/comp_mode", "f", sel_comp_mode);
579                 REGISTER_CALLBACK (serv, "/select/comp_makeup", "f", sel_comp_makeup);
580                 REGISTER_CALLBACK (serv, "/select/eq_enable", "f", sel_eq_enable);
581                 REGISTER_CALLBACK (serv, "/select/eq_hpf/freq", "f", sel_eq_hpf_freq);
582                 REGISTER_CALLBACK (serv, "/select/eq_hpf/enable", "f", sel_eq_hpf_enable);
583                 REGISTER_CALLBACK (serv, "/select/eq_hpf/slope", "f", sel_eq_hpf_slope);
584                 REGISTER_CALLBACK (serv, "/select/eq_lpf/freq", "f", sel_eq_lpf_freq);
585                 REGISTER_CALLBACK (serv, "/select/eq_lpf/enable", "f", sel_eq_lpf_enable);
586                 REGISTER_CALLBACK (serv, "/select/eq_lpf/slope", "f", sel_eq_lpf_slope);
587                 REGISTER_CALLBACK (serv, "/select/eq_gain", "if", sel_eq_gain);
588                 REGISTER_CALLBACK (serv, "/select/eq_freq", "if", sel_eq_freq);
589                 REGISTER_CALLBACK (serv, "/select/eq_q", "if", sel_eq_q);
590                 REGISTER_CALLBACK (serv, "/select/eq_shape", "if", sel_eq_shape);
591
592                 /* These commands require the route index in addition to the arg; TouchOSC (et al) can't use these  */
593                 REGISTER_CALLBACK (serv, "/strip/mute", "ii", route_mute);
594                 REGISTER_CALLBACK (serv, "/strip/solo", "ii", route_solo);
595                 REGISTER_CALLBACK (serv, "/strip/solo_iso", "ii", route_solo_iso);
596                 REGISTER_CALLBACK (serv, "/strip/solo_safe", "ii", route_solo_safe);
597                 REGISTER_CALLBACK (serv, "/strip/recenable", "ii", route_recenable);
598                 REGISTER_CALLBACK (serv, "/strip/record_safe", "ii", route_recsafe);
599                 REGISTER_CALLBACK (serv, "/strip/monitor_input", "ii", route_monitor_input);
600                 REGISTER_CALLBACK (serv, "/strip/monitor_disk", "ii", route_monitor_disk);
601                 REGISTER_CALLBACK (serv, "/strip/expand", "ii", strip_expand);
602                 REGISTER_CALLBACK (serv, "/strip/select", "ii", strip_gui_select);
603                 REGISTER_CALLBACK (serv, "/strip/polarity", "ii", strip_phase);
604                 REGISTER_CALLBACK (serv, "/strip/gain", "if", route_set_gain_dB);
605                 REGISTER_CALLBACK (serv, "/strip/fader", "if", route_set_gain_fader);
606                 REGISTER_CALLBACK (serv, "/strip/trimdB", "if", route_set_trim_dB);
607                 REGISTER_CALLBACK (serv, "/strip/pan_stereo_position", "if", route_set_pan_stereo_position);
608                 REGISTER_CALLBACK (serv, "/strip/pan_stereo_width", "if", route_set_pan_stereo_width);
609                 REGISTER_CALLBACK (serv, "/strip/plugin/parameter", "iiif", route_plugin_parameter);
610                 // prints to cerr only
611                 REGISTER_CALLBACK (serv, "/strip/plugin/parameter/print", "iii", route_plugin_parameter_print);
612                 REGISTER_CALLBACK (serv, "/strip/plugin/activate", "ii", route_plugin_activate);
613                 REGISTER_CALLBACK (serv, "/strip/plugin/deactivate", "ii", route_plugin_deactivate);
614                 REGISTER_CALLBACK (serv, "/strip/send/gain", "iif", route_set_send_gain_dB);
615                 REGISTER_CALLBACK (serv, "/strip/send/fader", "iif", route_set_send_fader);
616                 REGISTER_CALLBACK (serv, "/strip/send/enable", "iif", route_set_send_enable);
617                 REGISTER_CALLBACK(serv, "/strip/name", "is", route_rename);
618                 REGISTER_CALLBACK(serv, "/strip/sends", "i", route_get_sends);
619                 REGISTER_CALLBACK(serv, "/strip/receives", "i", route_get_receives);
620                 REGISTER_CALLBACK(serv, "/strip/plugin/list", "i", route_plugin_list);
621                 REGISTER_CALLBACK(serv, "/strip/plugin/descriptor", "ii", route_plugin_descriptor);
622                 REGISTER_CALLBACK(serv, "/strip/plugin/reset", "ii", route_plugin_reset);
623
624                 /* still not-really-standardized query interface */
625                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
626                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
627
628                 // un/register_update args= s:ctrl s:returl s:retpath
629                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
630                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
631                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
632                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
633
634                 /* this is a special catchall handler,
635                  * register at the end so this is only called if no
636                  * other handler matches (used for debug) */
637                 lo_server_add_method (serv, 0, 0, _catchall, this);
638         }
639 }
640
641 bool
642 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
643 {
644         if (ioc & ~IO_IN) {
645                 return false;
646         }
647
648         if (ioc & IO_IN) {
649                 lo_server_recv (srv);
650         }
651
652         return true;
653 }
654
655 std::string
656 OSC::get_server_url()
657 {
658         string url;
659         char * urlstr;
660
661         if (_osc_server) {
662                 urlstr = lo_server_get_url (_osc_server);
663                 url = urlstr;
664                 free (urlstr);
665         }
666
667         return url;
668 }
669
670 std::string
671 OSC::get_unix_server_url()
672 {
673         string url;
674         char * urlstr;
675
676         if (_osc_unix_server) {
677                 urlstr = lo_server_get_url (_osc_unix_server);
678                 url = urlstr;
679                 free (urlstr);
680         }
681
682         return url;
683 }
684
685 void
686 OSC::gui_changed ()
687 {
688         session->set_dirty();
689 }
690
691 void
692 OSC::listen_to_route (boost::shared_ptr<Stripable> strip, lo_address addr)
693 {
694         if (!strip) {
695                 return;
696         }
697         /* avoid duplicate listens */
698
699         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); ++x) {
700
701                 OSCRouteObserver* ro;
702
703                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
704
705                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
706
707                         if (ro->strip() == strip && res == 0) {
708                                 return;
709                         }
710                 }
711         }
712
713         OSCSurface *s = get_surface(addr);
714         uint32_t ssid = get_sid (strip, addr);
715         OSCRouteObserver* o = new OSCRouteObserver (strip, addr, ssid, s);
716         route_observers.push_back (o);
717
718         strip->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::route_lost, this, boost::weak_ptr<Stripable> (strip)), this);
719 }
720
721 void
722 OSC::route_lost (boost::weak_ptr<Stripable> wr)
723 {
724         tick = false;
725         drop_route (wr);
726         bank_dirty = true;
727 }
728
729 void
730 OSC::drop_route (boost::weak_ptr<Stripable> wr)
731 {
732         boost::shared_ptr<Stripable> r = wr.lock ();
733
734         if (!r) {
735                 return;
736         }
737
738         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
739
740                 OSCRouteObserver* rc;
741
742                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
743
744                         if (rc->strip() == r) {
745                                 delete *x;
746                                 x = route_observers.erase (x);
747                         } else {
748                                 ++x;
749                         }
750                 } else {
751                         ++x;
752                 }
753         }
754 }
755
756 void
757 OSC::end_listen (boost::shared_ptr<Stripable> r, lo_address addr)
758 {
759         RouteObservers::iterator x;
760
761         // Remove the route observers
762         for (x = route_observers.begin(); x != route_observers.end();) {
763
764                 OSCRouteObserver* ro;
765
766                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
767
768                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
769
770                         if (ro->strip() == r && res == 0) {
771                                 delete *x;
772                                 x = route_observers.erase (x);
773                         }
774                         else {
775                                 ++x;
776                         }
777                 }
778                 else {
779                         ++x;
780                 }
781         }
782 }
783
784 void
785 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
786 {
787         char* subpath;
788
789         subpath = (char*) malloc (len-15+1);
790         memcpy (subpath, path, len-15);
791         subpath[len-15] = '\0';
792
793         send_current_value (subpath, argv, argc, msg);
794
795         free (subpath);
796 }
797
798 void
799 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
800 {
801         if (!session) {
802                 return;
803         }
804
805         lo_message reply = lo_message_new ();
806         boost::shared_ptr<Route> r;
807         int id;
808
809         lo_message_add_string (reply, path);
810
811         if (argc == 0) {
812                 lo_message_add_string (reply, "bad syntax");
813         } else {
814                 id = argv[0]->i;
815                 r = session->get_remote_nth_route (id);
816
817                 if (!r) {
818                         lo_message_add_string (reply, "not found");
819                 } else {
820
821                         if (strcmp (path, "/strip/state") == 0) {
822
823                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
824                                         lo_message_add_string (reply, "AT");
825                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
826                                         lo_message_add_string (reply, "MT");
827                                 } else {
828                                         lo_message_add_string (reply, "B");
829                                 }
830
831                                 lo_message_add_string (reply, r->name().c_str());
832                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
833                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
834                                 lo_message_add_int32 (reply, r->muted());
835                                 lo_message_add_int32 (reply, r->soloed());
836
837                         } else if (strcmp (path, "/strip/mute") == 0) {
838
839                                 lo_message_add_int32 (reply, (float) r->muted());
840
841                         } else if (strcmp (path, "/strip/solo") == 0) {
842
843                                 lo_message_add_int32 (reply, r->soloed());
844                         }
845                 }
846         }
847         OSCSurface *sur = get_surface(get_address (msg));
848
849         if (sur->feedback[14]) {
850                 lo_send_message (get_address (msg), "/reply", reply);
851         } else {
852                 lo_send_message (get_address (msg), "#reply", reply);
853         }
854         lo_message_free (reply);
855 }
856
857 int
858 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
859 {
860         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
861 }
862
863 int
864 OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
865 {
866         size_t len;
867         int ret = 1; /* unhandled */
868
869         //cerr << "Received a message, path = " << path << " types = \""
870         //     << (types ? types : "NULL") << '"' << endl;
871
872         /* 15 for /#current_value plus 2 for /<path> */
873
874         len = strlen (path);
875         OSCSurface *sur = get_surface(get_address (msg));
876
877         if (strstr (path, "/automation")) {
878                 ret = set_automation (path, types, argv, argc, msg);
879
880         } else
881         if (strstr (path, "/touch")) {
882                 ret = touch_detect (path, types, argv, argc, msg);
883
884         } else
885         if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
886                 current_value_query (path, len, argv, argc, msg);
887                 ret = 0;
888
889         } else
890         if (!strncmp (path, "/cue/", 5)) {
891
892                 ret = cue_parse (path, types, argv, argc, msg);
893
894         } else
895         if (!strncmp (path, "/select/plugin/parameter", 24)) {
896
897                 ret = select_plugin_parameter (path, types, argv, argc, msg);
898
899         } else
900         if (!strncmp (path, "/access_action/", 15)) {
901                 check_surface (msg);
902                 if (!(argc && !argv[0]->i)) {
903                         std::string action_path = path;
904
905                         access_action (action_path.substr(15));
906                 }
907
908                 ret = 0;
909         } else
910         if (strcmp (path, "/strip/listen") == 0) {
911                 check_surface (msg);
912
913                 cerr << "set up listener\n";
914
915                 lo_message reply = lo_message_new ();
916
917                 if (argc <= 0) {
918                         lo_message_add_string (reply, "syntax error");
919                 } else {
920                         for (int n = 0; n < argc; ++n) {
921
922                                 boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
923
924                                 if (!r) {
925                                         lo_message_add_string (reply, "not found");
926                                         cerr << "no such route\n";
927                                         break;
928                                 } else {
929                                         cerr << "add listener\n";
930                                         listen_to_route (r, get_address (msg));
931                                         lo_message_add_int32 (reply, argv[n]->i);
932                                 }
933                         }
934                 }
935
936                 if (sur->feedback[14]) {
937                         lo_send_message (get_address (msg), "/reply", reply);
938                 } else {
939                         lo_send_message (get_address (msg), "#reply", reply);
940                 }
941                 lo_message_free (reply);
942
943                 ret = 0;
944
945         } else
946         if (strcmp (path, "/strip/ignore") == 0) {
947                 check_surface (msg);
948
949                 for (int n = 0; n < argc; ++n) {
950
951                         boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
952
953                         if (r) {
954                                 end_listen (r, get_address (msg));
955                         }
956                 }
957
958                 ret = 0;
959         } else
960         if (!strncmp (path, "/strip/gain/", 12) && strlen (path) > 12) {
961                 // in dB
962                 int ssid = atoi (&path[12]);
963                 ret = route_set_gain_dB (ssid, argv[0]->f, msg);
964         }
965         else if (!strncmp (path, "/strip/fader/", 13) && strlen (path) > 13) {
966                 // in fader position
967                 int ssid = atoi (&path[13]);
968                 ret = route_set_gain_fader (ssid, argv[0]->f, msg);
969         }
970         else if (!strncmp (path, "/strip/db_delta", 15)) {
971                 // in db delta
972                 int ssid;
973                 int ar_off = 0;
974                 float delta;
975                 if (strlen (path) > 15 && argc == 1) {
976                         ssid = atoi (&path[16]);
977                 } else if (argc == 2) {
978                         if (types[0] == 'f') {
979                                 ssid = (int) argv[0]->f;
980                         } else {
981                                 ssid = argv[0]->i;
982                         }
983                         ar_off = 1;
984                 } else {
985                         return -1;
986                 }
987                 if (types[ar_off] == 'f') {
988                         delta = argv[ar_off]->f;
989                 } else {
990                         delta = (float) argv[ar_off]->i;
991                 }
992                 ret = strip_db_delta (ssid, delta, msg);
993         }
994         else if (!strncmp (path, "/strip/trimdB/", 14) && strlen (path) > 14) {
995                 int ssid = atoi (&path[14]);
996                 ret = route_set_trim_dB (ssid, argv[0]->f, msg);
997         }
998         else if (!strncmp (path, "/strip/pan_stereo_position/", 27) && strlen (path) > 27) {
999                 int ssid = atoi (&path[27]);
1000                 ret = route_set_pan_stereo_position (ssid, argv[0]->f, msg);
1001         }
1002         else if (!strncmp (path, "/strip/mute/", 12) && strlen (path) > 12) {
1003                 int ssid = atoi (&path[12]);
1004                 ret = route_mute (ssid, argv[0]->i, msg);
1005         }
1006         else if (!strncmp (path, "/strip/solo/", 12) && strlen (path) > 12) {
1007                 int ssid = atoi (&path[12]);
1008                 ret = route_solo (ssid, argv[0]->i, msg);
1009         }
1010         else if (!strncmp (path, "/strip/monitor_input/", 21) && strlen (path) > 21) {
1011                 int ssid = atoi (&path[21]);
1012                 ret = route_monitor_input (ssid, argv[0]->i, msg);
1013         }
1014         else if (!strncmp (path, "/strip/monitor_disk/", 20) && strlen (path) > 20) {
1015                 int ssid = atoi (&path[20]);
1016                 ret = route_monitor_disk (ssid, argv[0]->i, msg);
1017         }
1018         else if (!strncmp (path, "/strip/recenable/", 17) && strlen (path) > 17) {
1019                 int ssid = atoi (&path[17]);
1020                 ret = route_recenable (ssid, argv[0]->i, msg);
1021         }
1022         else if (!strncmp (path, "/strip/record_safe/", 19) && strlen (path) > 19) {
1023                 int ssid = atoi (&path[19]);
1024                 ret = route_recsafe (ssid, argv[0]->i, msg);
1025         }
1026         else if (!strncmp (path, "/strip/expand/", 14) && strlen (path) > 14) {
1027                 int ssid = atoi (&path[14]);
1028                 ret = strip_expand (ssid, argv[0]->i, msg);
1029         }
1030         else if (!strncmp (path, "/strip/select/", 14) && strlen (path) > 14) {
1031                 int ssid = atoi (&path[14]);
1032                 ret = strip_gui_select (ssid, argv[0]->i, msg);
1033         }
1034         else if (!strncmp (path, "/select/send_gain/", 18) && strlen (path) > 18) {
1035                 int ssid = atoi (&path[18]);
1036                 ret = sel_sendgain (ssid, argv[0]->f, msg);
1037         }
1038         else if (!strncmp (path, "/select/send_fader/", 19) && strlen (path) > 19) {
1039                 int ssid = atoi (&path[19]);
1040                 ret = sel_sendfader (ssid, argv[0]->f, msg);
1041         }
1042         else if (!strncmp (path, "/select/send_enable/", 20) && strlen (path) > 20) {
1043                 int ssid = atoi (&path[20]);
1044                 ret = sel_sendenable (ssid, argv[0]->f, msg);
1045         }
1046         else if (!strncmp (path, "/select/eq_gain/", 16) && strlen (path) > 16) {
1047                 int ssid = atoi (&path[16]);
1048                 ret = sel_eq_gain (ssid, argv[0]->f, msg);
1049         }
1050         else if (!strncmp (path, "/select/eq_freq/", 16) && strlen (path) > 16) {
1051                 int ssid = atoi (&path[16]);
1052                 ret = sel_eq_freq (ssid, argv[0]->f , msg);
1053         }
1054         else if (!strncmp (path, "/select/eq_q/", 13) && strlen (path) > 13) {
1055                 int ssid = atoi (&path[13]);
1056                 ret = sel_eq_q (ssid, argv[0]->f, msg);
1057         }
1058         else if (!strncmp (path, "/select/eq_shape/", 17) && strlen (path) > 17) {
1059                 int ssid = atoi (&path[17]);
1060                 ret = sel_eq_shape (ssid, argv[0]->f, msg);
1061         }
1062         else if (!strncmp (path, "/set_surface", 12)) {
1063                 ret = surface_parse (path, types, argv, argc, msg);
1064         }
1065         if (ret) {
1066                 check_surface (msg);
1067         }
1068
1069         if ((ret && _debugmode != Off)) {
1070                 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
1071         } else if (!ret && _debugmode == All) {
1072                 debugmsg (_("OSC"), path, types, argv, argc);
1073         }
1074
1075         return ret;
1076 }
1077
1078 void
1079 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
1080 {
1081         std::stringstream ss;
1082         for (int i = 0; i < argc; ++i) {
1083                 lo_type type = (lo_type)types[i];
1084                         ss << " ";
1085                 switch (type) {
1086                         case LO_INT32:
1087                                 ss << "i:" << argv[i]->i;
1088                                 break;
1089                         case LO_FLOAT:
1090                                 ss << "f:" << argv[i]->f;
1091                                 break;
1092                         case LO_DOUBLE:
1093                                 ss << "d:" << argv[i]->d;
1094                                 break;
1095                         case LO_STRING:
1096                                 ss << "s:" << &argv[i]->s;
1097                                 break;
1098                         case LO_INT64:
1099                                 ss << "h:" << argv[i]->h;
1100                                 break;
1101                         case LO_CHAR:
1102                                 ss << "c:" << argv[i]->s;
1103                                 break;
1104                         case LO_TIMETAG:
1105                                 ss << "<Timetag>";
1106                                 break;
1107                         case LO_BLOB:
1108                                 ss << "<BLOB>";
1109                                 break;
1110                         case LO_TRUE:
1111                                 ss << "#T";
1112                                 break;
1113                         case LO_FALSE:
1114                                 ss << "#F";
1115                                 break;
1116                         case LO_NIL:
1117                                 ss << "NIL";
1118                                 break;
1119                         case LO_INFINITUM:
1120                                 ss << "#inf";
1121                                 break;
1122                         case LO_MIDI:
1123                                 ss << "<MIDI>";
1124                                 break;
1125                         case LO_SYMBOL:
1126                                 ss << "<SYMBOL>";
1127                                 break;
1128                         default:
1129                                 ss << "< ?? >";
1130                                 break;
1131                 }
1132         }
1133         PBD::info << prefix << ": " << path << ss.str() << endmsg;
1134 }
1135
1136 // "Application Hook" Handlers //
1137 void
1138 OSC::session_loaded (Session& s)
1139 {
1140 //      lo_address listener = lo_address_new (NULL, "7770");
1141 //      lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
1142 }
1143
1144 void
1145 OSC::session_exported (std::string path, std::string name)
1146 {
1147         lo_address listener = lo_address_new (NULL, "7770");
1148         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
1149         lo_address_free (listener);
1150 }
1151
1152 // end "Application Hook" Handlers //
1153
1154 /* path callbacks */
1155
1156 int
1157 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
1158 {
1159 #if 0
1160         const char* returl;
1161
1162         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
1163                 return 1;
1164         }
1165
1166         const char *returl = argv[1]->s;
1167         lo_address addr = find_or_cache_addr (returl);
1168
1169         const char *retpath = argv[2]->s;
1170
1171
1172         if (strcmp (argv[0]->s, "transport_frame") == 0) {
1173
1174                 if (session) {
1175                         lo_send (addr, retpath, "i", session->transport_frame());
1176                 }
1177
1178         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
1179
1180                 if (session) {
1181                         lo_send (addr, retpath, "i", session->transport_frame());
1182                 }
1183
1184         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
1185
1186                 if (session) {
1187                         lo_send (addr, retpath, "i", session->transport_frame());
1188                 }
1189
1190         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
1191
1192                 if (session) {
1193                         lo_send (addr, retpath, "i", session->transport_frame());
1194                 }
1195
1196         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
1197
1198                 if (session) {
1199                         lo_send (addr, retpath, "i", session->transport_frame());
1200                 }
1201
1202         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
1203
1204                 if (session) {
1205                         lo_send (addr, retpath, "i", session->transport_frame());
1206                 }
1207
1208         } else {
1209
1210                 /* error */
1211         }
1212 #endif
1213         return 0;
1214 }
1215
1216 void
1217 OSC::routes_list (lo_message msg)
1218 {
1219         if (!session) {
1220                 return;
1221         }
1222         OSCSurface *sur = get_surface(get_address (msg));
1223         sur->no_clear = true;
1224
1225         for (int n = 0; n < (int) sur->nstrips; ++n) {
1226
1227                 boost::shared_ptr<Stripable> s = get_strip (n + 1, get_address (msg));
1228
1229                 if (s) {
1230                         // some things need the route
1231                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
1232
1233                         lo_message reply = lo_message_new ();
1234
1235                         if (s->presentation_info().flags() & PresentationInfo::AudioTrack) {
1236                                 lo_message_add_string (reply, "AT");
1237                         } else if (s->presentation_info().flags() & PresentationInfo::MidiTrack) {
1238                                 lo_message_add_string (reply, "MT");
1239                         } else if (s->presentation_info().flags() & PresentationInfo::AudioBus) {
1240                                 // r->feeds (session->master_out()) may make more sense
1241                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
1242                                         // this is a bus
1243                                         lo_message_add_string (reply, "B");
1244                                 } else {
1245                                         // this is an Aux out
1246                                         lo_message_add_string (reply, "AX");
1247                                 }
1248                         } else if (s->presentation_info().flags() & PresentationInfo::MidiBus) {
1249                                 lo_message_add_string (reply, "MB");
1250                         } else if (s->presentation_info().flags() & PresentationInfo::VCA) {
1251                                 lo_message_add_string (reply, "V");
1252                         }
1253
1254                         lo_message_add_string (reply, s->name().c_str());
1255                         if (r) {
1256                                 // routes have inputs and outputs
1257                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
1258                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
1259                         } else {
1260                                 // non-routes like VCAs don't
1261                                 lo_message_add_int32 (reply, 0);
1262                                 lo_message_add_int32 (reply, 0);
1263                         }
1264                         if (s->mute_control()) {
1265                                 lo_message_add_int32 (reply, s->mute_control()->get_value());
1266                         } else {
1267                                 lo_message_add_int32 (reply, 0);
1268                         }
1269                         if (s->solo_control()) {
1270                                 lo_message_add_int32 (reply, s->solo_control()->get_value());
1271                         } else {
1272                                 lo_message_add_int32 (reply, 0);
1273                         }
1274                         lo_message_add_int32 (reply, n + 1);
1275                         if (s->rec_enable_control()) {
1276                                 lo_message_add_int32 (reply, s->rec_enable_control()->get_value());
1277                         }
1278
1279                         //Automatically listen to stripables listed
1280                         listen_to_route(s, get_address (msg));
1281
1282                         if (sur->feedback[14]) {
1283                                 lo_send_message (get_address (msg), "/reply", reply);
1284                         } else {
1285                                 lo_send_message (get_address (msg), "#reply", reply);
1286                         }
1287                         lo_message_free (reply);
1288                 }
1289         }
1290
1291         // Send end of listing message
1292         lo_message reply = lo_message_new ();
1293
1294         lo_message_add_string (reply, "end_route_list");
1295         lo_message_add_int64 (reply, session->frame_rate());
1296         lo_message_add_int64 (reply, session->current_end_frame());
1297         if (session->monitor_out()) {
1298                 // this session has a monitor section
1299                 lo_message_add_int32 (reply, 1);
1300         } else {
1301                 lo_message_add_int32 (reply, 0);
1302         }
1303
1304         if (sur->feedback[14]) {
1305                 lo_send_message (get_address (msg), "/reply", reply);
1306         } else {
1307                 lo_send_message (get_address (msg), "#reply", reply);
1308         }
1309
1310         lo_message_free (reply);
1311 }
1312
1313 int
1314 OSC::cancel_all_solos ()
1315 {
1316         session->cancel_all_solo ();
1317         return 0;
1318 }
1319
1320 lo_address
1321 OSC::get_address (lo_message msg)
1322 {
1323         if (address_only) {
1324                 lo_address addr = lo_message_get_source (msg);
1325                 string host = lo_address_get_hostname (addr);
1326                 int protocol = lo_address_get_protocol (addr);
1327                 return lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1328         } else {
1329                 return lo_message_get_source (msg);
1330         }
1331 }
1332
1333 int
1334 OSC::refresh_surface (lo_message msg)
1335 {
1336         OSCSurface *s = get_surface(get_address (msg));
1337         // restart all observers
1338         set_surface (s->bank_size, (uint32_t) s->strip_types.to_ulong(), (uint32_t) s->feedback.to_ulong(), \
1339                 (uint32_t) s->gainmode, (uint32_t) s->send_page_size, (uint32_t) s->plug_page_size, msg);
1340         return 0;
1341 }
1342
1343 void
1344 OSC::clear_devices ()
1345 {
1346         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
1347
1348                 OSCRouteObserver* rc;
1349
1350                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
1351                         delete *x;
1352                         x = route_observers.erase (x);
1353                 } else {
1354                         ++x;
1355                 }
1356                 // slow devices need time to clear buffers
1357                 usleep ((uint32_t) 10);
1358         }
1359         // Should maybe do global_observers too
1360         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
1361
1362                 OSCGlobalObserver* gc;
1363
1364                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1365                         delete *x;
1366                         x = global_observers.erase (x);
1367                 } else {
1368                         ++x;
1369                 }
1370         }
1371         // delete select observers
1372         for (uint32_t it = 0; it < _surface.size(); ++it) {
1373                 OSCSurface* sur = &_surface[it];
1374                 OSCSelectObserver* so;
1375                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
1376                         delete so;
1377                 }
1378         }
1379         // delete cue observers
1380         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
1381                 OSCCueObserver* co;
1382                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
1383                         delete *x;
1384                         x = cue_observers.erase (x);
1385                 } else {
1386                         ++x;
1387                 }
1388         }
1389
1390         // clear out surfaces
1391         _surface.clear();
1392 }
1393
1394 int
1395 OSC::surface_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
1396 {
1397         int ret = 1; /* unhandled */
1398         OSCSurface *sur = get_surface(get_address (msg));
1399         int pi_page = sur->plug_page_size;
1400         int se_page = sur->send_page_size;
1401         int fadermode = sur->gainmode;
1402         int feedback = sur->feedback.to_ulong();
1403         int strip_types = sur->strip_types.to_ulong();
1404         int bank_size = sur->bank_size;
1405
1406
1407         if (!strncmp (path, "/set_surface/feedback", 21)) {
1408                 if (argv[0]->f) {
1409                         ret = set_surface_feedback ((int)argv[0]->f, msg);
1410                 } else {
1411                         ret = set_surface_feedback (argv[0]->i, msg);
1412                 }
1413         }
1414         else if (!strncmp (path, "/set_surface/bank_size", 22)) {
1415                 if (argv[0]->f) {
1416                         ret = set_surface_bank_size ((int)argv[0]->f, msg);
1417                 } else {
1418                         ret = set_surface_bank_size (argv[0]->i, msg);
1419                 }
1420         }
1421         else if (!strncmp (path, "/set_surface/gainmode", 21)) {
1422                 if (argv[0]->f) {
1423                         ret = set_surface_gainmode ((int)argv[0]->f, msg);
1424                 } else {
1425                         ret = set_surface_gainmode (argv[0]->i, msg);
1426                 }
1427         }
1428         else if (!strncmp (path, "/set_surface/strip_types", 24)) {
1429                 if (argv[0]->f) {
1430                         ret = set_surface_strip_types ((int)argv[0]->f, msg);
1431                 } else {
1432                         ret = set_surface_strip_types (argv[0]->i, msg);
1433                 }
1434         }
1435         else if (!strncmp (path, "/set_surface/send_page_size", 27)) {
1436                 if (argv[0]->f) {
1437                         ret = sel_send_pagesize ((int)argv[0]->f, msg);
1438                 } else {
1439                         ret = sel_send_pagesize (argv[0]->i, msg);
1440                 }
1441         }
1442         else if (!strncmp (path, "/set_surface/plugin_page_size", 29)) {
1443                 if (argv[0]->f) {
1444                         ret = sel_plug_pagesize ((int)argv[0]->f, msg);
1445                 } else {
1446                         ret = sel_plug_pagesize (argv[0]->i, msg);
1447                 }
1448         } else if (strlen(path) == 12) {
1449                 // command is in /set_surface iii form
1450                 switch (argc) {
1451                         case 6:
1452                                 if (argv[5]->f) {
1453                                         pi_page = (int) argv[5]->f;
1454                                 } else {
1455                                         pi_page = argv[5]->i;
1456                                 }
1457                         case 5:
1458                                 if (argv[4]->f) {
1459                                         se_page = (int) argv[4]->f;
1460                                 } else {
1461                                         se_page = argv[4]->i;
1462                                 }
1463                         case 4:
1464                                 if (argv[3]->f) {
1465                                         fadermode = (int) argv[3]->f;
1466                                 } else {
1467                                         fadermode = argv[3]->i;
1468                                 }
1469                         case 3:
1470                                 if (argv[2]->f) {
1471                                         feedback = (int) argv[2]->f;
1472                                 } else {
1473                                         feedback = argv[2]->i;
1474                                 }
1475                         case 2:
1476                                 if (argv[1]->f) {
1477                                         strip_types = (int) argv[1]->f;
1478                                 } else {
1479                                         strip_types = argv[1]->i;
1480                                 }
1481                         case 1:
1482                                 if (argv[0]->f) {
1483                                         bank_size = (int) argv[0]->f;
1484                                 } else {
1485                                         bank_size = argv[0]->i;
1486                                 }
1487                                 ret = set_surface (bank_size, strip_types, feedback, fadermode, se_page, pi_page, msg);
1488                                 break;
1489                         case 0:
1490                                 // send current setup
1491                                 {
1492                                         lo_message reply = lo_message_new ();
1493                                         lo_message_add_int32 (reply, bank_size);
1494                                         lo_message_add_int32 (reply, strip_types);
1495                                         lo_message_add_int32 (reply, feedback);
1496                                         lo_message_add_int32 (reply, fadermode);
1497                                         lo_message_add_int32 (reply, se_page);
1498                                         lo_message_add_int32 (reply, pi_page);
1499                                         lo_send_message (get_address (msg), "/set_surface", reply);
1500                                         lo_message_free (reply);
1501                                         return 0;
1502                                 }
1503                                 break;
1504
1505                         default:
1506                                 PBD::warning << "OSC: Too many parameters." << endmsg;
1507                                 return 1;
1508                                 break;
1509                 }
1510         } else if (isdigit(path[13])) {
1511                 // some of our parameters must be "in-lined"
1512                 bank_size = atoi (&path[13]);
1513                 const char * par = strstr (&path[13], "/");
1514                 if (par) {
1515                         strip_types = atoi (&par[1]);
1516                         const char * fb = strstr (&par[1], "/");
1517                         if (fb) {
1518                                 feedback = atoi (&fb[1]);
1519                                 const char * fm = strstr (&fb[1], "/");
1520                                 if (fm) {
1521                                         fadermode = atoi (&fm[1]);
1522                                         const char * sp = strstr (&fm[1], "/");
1523                                         if (sp) {
1524                                                 se_page = atoi (&sp[1]);
1525                                                 const char * pp = strstr (&sp[1], "/");
1526                                                 if (pp) {
1527                                                         pi_page = atoi (&pp[1]);
1528                                                 } else {
1529                                                         if (argv[0]->f) {
1530                                                                 pi_page = (int) argv[0]->f;
1531                                                         } else if (argv[0]->i) {
1532                                                                 pi_page = argv[0]->i;
1533                                                         }
1534                                                 }
1535                                         } else {
1536                                                 if (argv[0]->f) {
1537                                                         se_page = (int) argv[0]->f;
1538                                                 } else if (argv[0]->i) {
1539                                                         se_page = argv[0]->i;
1540                                                 }
1541                                         }
1542                                 } else {
1543                                         if (argv[0]->f) {
1544                                                 fadermode = (int) argv[0]->f;
1545                                         } else if (argv[0]->i) {
1546                                                 fadermode = argv[0]->i;
1547                                         }
1548                                 }
1549                         } else {
1550                                 if (argv[0]->f) {
1551                                         feedback = (int) argv[0]->f;
1552                                 } else if (argv[0]->i) {
1553                                         feedback = argv[0]->i;
1554                                 }
1555                         }
1556                 } else {
1557                         if (argv[0]->f) {
1558                                 strip_types = (int) argv[0]->f;
1559                         } else if (argv[0]->i) {
1560                                 strip_types = argv[0]->i;
1561                         }
1562                 }
1563                 ret = set_surface (bank_size, strip_types, feedback, fadermode, se_page, pi_page, msg);
1564         }
1565         return ret;
1566 }
1567
1568 int
1569 OSC::set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gm, uint32_t se_size, uint32_t pi_size, lo_message msg)
1570 {
1571         OSCSurface *s = get_surface(get_address (msg));
1572         s->bank_size = b_size;
1573         s->strip_types = strips;
1574         s->feedback = fb;
1575         s->gainmode = gm;
1576         s->send_page_size = se_size;
1577         s->plug_page_size = pi_size;
1578         // set bank and strip feedback
1579         set_bank(s->bank, msg);
1580
1581         global_feedback (s->feedback, get_address (msg), s->gainmode);
1582         sel_send_pagesize (se_size, msg);
1583         sel_plug_pagesize (pi_size, msg);
1584         return 0;
1585 }
1586
1587 int
1588 OSC::set_surface_bank_size (uint32_t bs, lo_message msg)
1589 {
1590         OSCSurface *s = get_surface(get_address (msg));
1591         s->bank_size = bs;
1592
1593         // set bank and strip feedback
1594         set_bank(s->bank, msg);
1595         return 0;
1596 }
1597
1598 int
1599 OSC::set_surface_strip_types (uint32_t st, lo_message msg)
1600 {
1601         OSCSurface *s = get_surface(get_address (msg));
1602         s->strip_types = st;
1603
1604         // set bank and strip feedback
1605         set_bank(s->bank, msg);
1606         return 0;
1607 }
1608
1609
1610 int
1611 OSC::set_surface_feedback (uint32_t fb, lo_message msg)
1612 {
1613         OSCSurface *s = get_surface(get_address (msg));
1614         s->feedback = fb;
1615
1616         // set bank and strip feedback
1617         set_bank(s->bank, msg);
1618
1619         // Set global/master feedback
1620         global_feedback (s->feedback, get_address (msg), s->gainmode);
1621         return 0;
1622 }
1623
1624 int
1625 OSC::set_surface_gainmode (uint32_t gm, lo_message msg)
1626 {
1627         OSCSurface *s = get_surface(get_address (msg));
1628         s->gainmode = gm;
1629
1630         // set bank and strip feedback
1631         set_bank(s->bank, msg);
1632
1633         // Set global/master feedback
1634         global_feedback (s->feedback, get_address (msg), s->gainmode);
1635         return 0;
1636 }
1637
1638 int
1639 OSC::check_surface (lo_message msg)
1640 {
1641         if (!session) {
1642                 return -1;
1643         }
1644         get_surface(get_address (msg));
1645         return 0;
1646 }
1647
1648 OSC::OSCSurface *
1649 OSC::get_surface (lo_address addr)
1650 {
1651         string r_url;
1652         char * rurl;
1653         if (address_only) {
1654                 string host = lo_address_get_hostname (addr);
1655                 int protocol = lo_address_get_protocol (addr);
1656                 addr = lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1657         }
1658
1659         rurl = lo_address_get_url (addr);
1660         r_url = rurl;
1661         free (rurl);
1662         for (uint32_t it = 0; it < _surface.size(); ++it) {
1663                 //find setup for this server
1664                 if (!_surface[it].remote_url.find(r_url)){
1665                         return &_surface[it];
1666                 }
1667         }
1668
1669         // No surface create one with default values
1670         OSCSurface s;
1671         s.remote_url = r_url;
1672         s.no_clear = false;
1673         s.jogmode = JOG;
1674         s.bank = 1;
1675         s.bank_size = default_banksize; // need to find out how many strips there are
1676         s.strip_types = default_strip; // 159 is tracks, busses, and VCAs (no master/monitor)
1677         s.feedback = default_feedback;
1678         s.gainmode = default_gainmode;
1679         s.sel_obs = 0;
1680         s.expand = 0;
1681         s.expand_enable = false;
1682         s.cue = false;
1683         s.aux = 0;
1684         s.strips = get_sorted_stripables(s.strip_types, s.cue);
1685         s.send_page = 1;
1686         s.send_page_size = default_send_size;
1687         s.plug_page = 1;
1688         s.plug_page_size = default_plugin_size;
1689         s.plugin_id = 1;
1690
1691         s.nstrips = s.strips.size();
1692         _surface.push_back (s);
1693         // moved this down here as selection may need s.<anything to do with select> set
1694         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1695                 gui_selection_changed();
1696         }
1697
1698         // set bank and strip feedback
1699         _set_bank(s.bank, addr);
1700
1701         // Set global/master feedback
1702         global_feedback (s.feedback, addr, s.gainmode);
1703
1704         return &_surface[_surface.size() - 1];
1705 }
1706
1707 // setup global feedback for a surface
1708 void
1709 OSC::global_feedback (bitset<32> feedback, lo_address addr, uint32_t gainmode)
1710 {
1711         // first destroy global observer for this surface
1712         GlobalObservers::iterator x;
1713         for (x = global_observers.begin(); x != global_observers.end();) {
1714
1715                 OSCGlobalObserver* ro;
1716
1717                 if ((ro = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1718
1719                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
1720
1721                         if (res == 0) {
1722                                 delete *x;
1723                                 x = global_observers.erase (x);
1724                         } else {
1725                                 ++x;
1726                         }
1727                 } else {
1728                         ++x;
1729                 }
1730         }
1731         if (feedback[4] || feedback[3] || feedback[5] || feedback[6]) {
1732                 // create a new Global Observer for this surface
1733                 OSCGlobalObserver* o = new OSCGlobalObserver (*session, addr, gainmode, /*s->*/feedback);
1734                 global_observers.push_back (o);
1735         }
1736 }
1737
1738 void
1739 OSC::notify_routes_added (ARDOUR::RouteList &)
1740 {
1741         // not sure if we need this PI change seems to cover
1742         //recalcbanks();
1743 }
1744
1745 void
1746 OSC::notify_vca_added (ARDOUR::VCAList &)
1747 {
1748         // not sure if we need this PI change seems to cover
1749         //recalcbanks();
1750 }
1751
1752 void
1753 OSC::recalcbanks ()
1754 {
1755         tick = false;
1756         bank_dirty = true;
1757 }
1758
1759 void
1760 OSC::_recalcbanks ()
1761 {
1762         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1763                 _select = ControlProtocol::first_selected_stripable();
1764         }
1765
1766         // do a set_bank for each surface we know about.
1767         for (uint32_t it = 0; it < _surface.size(); ++it) {
1768                 OSCSurface* sur = &_surface[it];
1769                 // find lo_address
1770                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
1771                 if (sur->cue) {
1772                         _cue_set (sur->aux, addr);
1773                 } else {
1774                         _set_bank (sur->bank, addr);
1775                 }
1776                 if (sur->no_clear) {
1777                         // This surface uses /strip/list tell it routes have changed
1778                         lo_message reply;
1779                         reply = lo_message_new ();
1780                         lo_send_message (addr, "/strip/list", reply);
1781                         lo_message_free (reply);
1782                 }
1783         }
1784 }
1785
1786 /*
1787  * This gets called not only when bank changes but also:
1788  *  - bank size change
1789  *  - feedback change
1790  *  - strip types changes
1791  *  - fadermode changes
1792  *  - stripable creation/deletion/flag
1793  *  - to refresh what is "displayed"
1794  * Basically any time the bank needs to be rebuilt
1795  */
1796 int
1797 OSC::set_bank (uint32_t bank_start, lo_message msg)
1798 {
1799         return _set_bank (bank_start, get_address (msg));
1800 }
1801
1802 // set bank is callable with either message or address
1803 int
1804 OSC::_set_bank (uint32_t bank_start, lo_address addr)
1805 {
1806         if (!session) {
1807                 return -1;
1808         }
1809         // no nstripables yet
1810         if (!session->nroutes()) {
1811                 return -1;
1812         }
1813
1814         OSCSurface *s = get_surface (addr);
1815
1816         // revert any expand to select
1817          s->expand = 0;
1818          s->expand_enable = false;
1819         _strip_select (ControlProtocol::first_selected_stripable(), addr);
1820
1821         // undo all listeners for this url
1822         StripableList stripables;
1823         session->get_stripables (stripables);
1824         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
1825
1826                 boost::shared_ptr<Stripable> stp = *it;
1827                 if (stp) {
1828                         end_listen (stp, addr);
1829                 }
1830                 // slow devices need time to clear buffers
1831                 usleep ((uint32_t) 10);
1832         }
1833
1834         s->strips = get_sorted_stripables(s->strip_types, s->cue);
1835         s->nstrips = s->strips.size();
1836
1837         uint32_t b_size;
1838         if (!s->bank_size) {
1839                 // no banking - bank includes all stripables
1840                 b_size = s->nstrips;
1841         } else {
1842                 b_size = s->bank_size;
1843         }
1844
1845         // Do limits checking
1846         if (bank_start < 1) bank_start = 1;
1847         if (b_size >= s->nstrips)  {
1848                 bank_start = 1;
1849         } else if (bank_start > ((s->nstrips - b_size) + 1)) {
1850                 // top bank is always filled if there are enough strips for at least one bank
1851                 bank_start = (uint32_t)((s->nstrips - b_size) + 1);
1852         }
1853         //save bank after bank limit checks
1854         s->bank = bank_start;
1855
1856         if (s->feedback[0] || s->feedback[1]) {
1857
1858                 for (uint32_t n = bank_start; n < (min ((b_size + bank_start), s->nstrips + 1)); ++n) {
1859                         if (n <= s->strips.size()) {
1860                                 boost::shared_ptr<Stripable> stp = s->strips[n - 1];
1861
1862                                 if (stp) {
1863                                         listen_to_route(stp, addr);
1864                                 }
1865                         }
1866                         // slow devices need time to clear buffers
1867                         usleep ((uint32_t) 20);
1868                 }
1869         }
1870         // light bankup or bankdown buttons if it is possible to bank in that direction
1871         if (s->feedback[4] && !s->no_clear) {
1872                 lo_message reply;
1873                 reply = lo_message_new ();
1874                 if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
1875                         lo_message_add_int32 (reply, 0);
1876                 } else {
1877                         lo_message_add_int32 (reply, 1);
1878                 }
1879                 lo_send_message (addr, "/bank_up", reply);
1880                 lo_message_free (reply);
1881                 reply = lo_message_new ();
1882                 if (s->bank > 1) {
1883                         lo_message_add_int32 (reply, 1);
1884                 } else {
1885                         lo_message_add_int32 (reply, 0);
1886                 }
1887                 lo_send_message (addr, "/bank_down", reply);
1888                 lo_message_free (reply);
1889         }
1890         bank_dirty = false;
1891         tick = true;
1892         return 0;
1893 }
1894
1895 int
1896 OSC::bank_up (lo_message msg)
1897 {
1898         if (!session) {
1899                 return -1;
1900         }
1901         OSCSurface *s = get_surface(get_address (msg));
1902         set_bank (s->bank + s->bank_size, msg);
1903         return 0;
1904 }
1905
1906 int
1907 OSC::bank_delta (float delta, lo_message msg)
1908 {
1909         if (!session) {
1910                 return -1;
1911         }
1912         OSCSurface *s = get_surface(get_address (msg));
1913         uint32_t new_bank = s->bank + (s->bank_size * (int) delta);
1914         if ((int)new_bank < 1) {
1915                 new_bank = 1;
1916         }
1917         if (new_bank != s->bank) {
1918                 set_bank (new_bank, msg);
1919         }
1920         return 0;
1921 }
1922
1923 int
1924 OSC::bank_down (lo_message msg)
1925 {
1926         if (!session) {
1927                 return -1;
1928         }
1929         OSCSurface *s = get_surface(get_address (msg));
1930         if (s->bank < s->bank_size) {
1931                 set_bank (1, msg);
1932         } else {
1933                 set_bank (s->bank - s->bank_size, msg);
1934         }
1935         return 0;
1936 }
1937
1938 uint32_t
1939 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
1940 {
1941         if (!strip) {
1942                 return 0;
1943         }
1944
1945         OSCSurface *s = get_surface(addr);
1946
1947         uint32_t b_size;
1948         if (!s->bank_size) {
1949                 // no banking
1950                 b_size = s->nstrips;
1951         } else {
1952                 b_size = s->bank_size;
1953         }
1954
1955         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
1956                 if (n <= s->strips.size()) {
1957                         if (strip == s->strips[n-1]) {
1958                                 return n - s->bank + 1;
1959                         }
1960                 }
1961         }
1962         // failsafe... should never get here.
1963         return 0;
1964 }
1965
1966 boost::shared_ptr<ARDOUR::Stripable>
1967 OSC::get_strip (uint32_t ssid, lo_address addr)
1968 {
1969         OSCSurface *s = get_surface(addr);
1970         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
1971                 return s->strips[ssid + s->bank - 2];
1972         }
1973         // guess it is out of range
1974         return boost::shared_ptr<ARDOUR::Stripable>();
1975 }
1976
1977 // send and plugin paging commands
1978 int
1979 OSC::sel_send_pagesize (uint32_t size, lo_message msg)
1980 {
1981         OSCSurface *s = get_surface(get_address (msg));
1982         if  (size != s->send_page_size) {
1983                 s->send_page_size = size;
1984                 s->sel_obs->renew_sends();
1985         }
1986         return 0;
1987 }
1988
1989 int
1990 OSC::sel_send_page (int page, lo_message msg)
1991 {
1992         OSCSurface *s = get_surface(get_address (msg));
1993         s->send_page = s->send_page + page;
1994         s->sel_obs->renew_sends();
1995         return 0;
1996 }
1997
1998 int
1999 OSC::sel_plug_pagesize (uint32_t size, lo_message msg)
2000 {
2001         OSCSurface *s = get_surface(get_address (msg));
2002         if (size != s->plug_page_size) {
2003                 s->plug_page_size = size;
2004                 s->sel_obs->renew_plugin();
2005         }
2006         return 0;
2007 }
2008
2009 int
2010 OSC::sel_plug_page (int page, lo_message msg)
2011 {
2012         OSCSurface *s = get_surface(get_address (msg));
2013         s->plug_page = s->plug_page + page;
2014         s->sel_obs->renew_plugin();
2015         return 0;
2016 }
2017
2018 int
2019 OSC::sel_plugin (int delta, lo_message msg)
2020 {
2021         OSCSurface *sur = get_surface(get_address (msg));
2022         return _sel_plugin (sur->plugin_id + delta, get_address (msg));
2023 }
2024
2025 int
2026 OSC::_sel_plugin (int id, lo_address addr)
2027 {
2028         OSCSurface *sur = get_surface(addr);
2029         boost::shared_ptr<Stripable> s;
2030         if (sur->expand_enable) {
2031                 s = get_strip (sur->expand, addr);
2032         } else {
2033                 s = _select;
2034         }
2035         if (s) {
2036                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
2037                 if (!r) {
2038                         return 1;
2039                 }
2040
2041                 // find out how many plugins we have
2042                 bool plugs;
2043                 int nplugs  = 0;
2044                 sur->plugins.clear();
2045                 do {
2046                         plugs = false;
2047                         if (r->nth_plugin (nplugs)) {
2048                                 if (r->nth_plugin(nplugs)->display_to_user()) {
2049 #ifdef MIXBUS
2050                                         // need to check for mixbus channel strips (and exclude them)
2051                                         boost::shared_ptr<Processor> proc = r->nth_plugin (nplugs);
2052                                         boost::shared_ptr<PluginInsert> pi;
2053                                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
2054
2055                                                 if (!pi->is_channelstrip()) {
2056 #endif
2057                                                         sur->plugins.push_back (nplugs);
2058 #ifdef MIXBUS
2059                                                 }
2060                                         }
2061 #endif
2062                                 }
2063                                 plugs = true;
2064                                 nplugs++;
2065                         }
2066                 } while (plugs);
2067
2068                 // limit plugin_id to actual plugins
2069                 if (!sur->plugins.size()) {
2070                         sur->plugin_id = 0;
2071                         return 0;
2072                 } else if (sur->plugins.size() < (uint32_t) id) {
2073                         sur->plugin_id = sur->plugins.size();
2074                 } else  if (sur->plugins.size() && !id) {
2075                         sur->plugin_id = 1;
2076                 } else {
2077                         sur->plugin_id = id;
2078                 }
2079
2080                 // we have a plugin number now get the processor
2081                 boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
2082                 boost::shared_ptr<PluginInsert> pi;
2083                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
2084                         PBD::warning << "OSC: Plugin: " << sur->plugin_id << " does not seem to be a plugin" << endmsg;                 
2085                         return 1;
2086                 }
2087                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2088                 bool ok = false;
2089                 // put only input controls into a vector
2090                 sur->plug_params.clear ();
2091                 uint32_t nplug_params  = pip->parameter_count();
2092                 for ( uint32_t ppi = 0;  ppi < nplug_params; ++ppi) {
2093                         uint32_t controlid = pip->nth_parameter(ppi, ok);
2094                         if (!ok) {
2095                                 continue;
2096                         }
2097                         if (pip->parameter_is_input(controlid)) {
2098                                 sur->plug_params.push_back (ppi);
2099                         }
2100                 }
2101
2102                 sur->plug_page = 1;
2103
2104                 if (sur->sel_obs) {
2105                         sur->sel_obs->renew_plugin();
2106                 }
2107                 return 0;
2108         }
2109         return 1;
2110 }
2111
2112 void
2113 OSC::transport_frame (lo_message msg)
2114 {
2115         if (!session) {
2116                 return;
2117         }
2118         check_surface (msg);
2119         framepos_t pos = session->transport_frame ();
2120
2121         lo_message reply = lo_message_new ();
2122         lo_message_add_int64 (reply, pos);
2123
2124         lo_send_message (get_address (msg), "/transport_frame", reply);
2125
2126         lo_message_free (reply);
2127 }
2128
2129 void
2130 OSC::transport_speed (lo_message msg)
2131 {
2132         if (!session) {
2133                 return;
2134         }
2135         check_surface (msg);
2136         double ts = session->transport_speed ();
2137
2138         lo_message reply = lo_message_new ();
2139         lo_message_add_double (reply, ts);
2140
2141         lo_send_message (get_address (msg), "/transport_speed", reply);
2142
2143         lo_message_free (reply);
2144 }
2145
2146 void
2147 OSC::record_enabled (lo_message msg)
2148 {
2149         if (!session) {
2150                 return;
2151         }
2152         check_surface (msg);
2153         int re = (int)session->get_record_enabled ();
2154
2155         lo_message reply = lo_message_new ();
2156         lo_message_add_int32 (reply, re);
2157
2158         lo_send_message (get_address (msg), "/record_enabled", reply);
2159
2160         lo_message_free (reply);
2161 }
2162
2163 int
2164 OSC::scrub (float delta, lo_message msg)
2165 {
2166         if (!session) return -1;
2167         check_surface (msg);
2168
2169         scrub_place = session->transport_frame ();
2170
2171         float speed;
2172
2173         int64_t now = ARDOUR::get_microseconds ();
2174         int64_t diff = now - scrub_time;
2175         if (diff > 35000) {
2176                 // speed 1 (or 0 if jog wheel supports touch)
2177                 speed = delta;
2178         } else if ((diff > 20000) && (fabs(scrub_speed) == 1)) {
2179                 // add some hysteresis to stop excess speed jumps
2180                 speed = delta;
2181         } else {
2182                 speed = (int)(delta * 2);
2183         }
2184         scrub_time = now;
2185         if (scrub_speed == speed) {
2186                 // Already at that speed no change
2187                 return 0;
2188         }
2189         scrub_speed = speed;
2190
2191         if (speed > 0) {
2192                 if (speed == 1) {
2193                         session->request_transport_speed (.5);
2194                 } else {
2195                         session->request_transport_speed (1);
2196                 }
2197         } else if (speed < 0) {
2198                 if (speed == -1) {
2199                         session->request_transport_speed (-.5);
2200                 } else {
2201                         session->request_transport_speed (-1);
2202                 }
2203         } else {
2204                 session->request_transport_speed (0);
2205         }
2206
2207         return 0;
2208 }
2209
2210 int
2211 OSC::jog (float delta, lo_message msg)
2212 {
2213         if (!session) return -1;
2214
2215         OSCSurface *s = get_surface(get_address (msg));
2216
2217         string path = "/jog/mode/name";
2218         switch(s->jogmode)
2219         {
2220                 case JOG  :
2221                         text_message (path, "Jog", get_address (msg));
2222                         if (delta) {
2223                                 jump_by_seconds (delta / 5);
2224                         }
2225                         break;
2226                 case SCRUB:
2227                         text_message (path, "Scrub", get_address (msg));
2228                         scrub (delta, msg);
2229                         break;
2230                 case SHUTTLE:
2231                         text_message (path, "Shuttle", get_address (msg));
2232                         if (delta) {
2233                                 double speed = get_transport_speed ();
2234                                 set_transport_speed (speed + (delta / 8));
2235                         } else {
2236                                 set_transport_speed (0);
2237                         }
2238                         break;
2239                 case SCROLL:
2240                         text_message (path, "Scroll", get_address (msg));
2241                         if (delta > 0) {
2242                                 access_action ("Editor/scroll-forward");
2243                         } else if (delta < 0) {
2244                                 access_action ("Editor/scroll-backward");
2245                         }
2246                         break;
2247                 case TRACK:
2248                         text_message (path, "Track", get_address (msg));
2249                         if (delta > 0) {
2250                                 set_bank (s->bank + 1, msg);
2251                         } else if (delta < 0) {
2252                                 set_bank (s->bank - 1, msg);
2253                         }
2254                         break;
2255                 case BANK:
2256                         text_message (path, "Bank", get_address (msg));
2257                         if (delta > 0) {
2258                                 bank_up (msg);
2259                         } else if (delta < 0) {
2260                                 bank_down (msg);
2261                         }
2262                         break;
2263                 case NUDGE:
2264                         text_message (path, "Nudge", get_address (msg));
2265                         if (delta > 0) {
2266                                 access_action ("Common/nudge-playhead-forward");
2267                         } else if (delta < 0) {
2268                                 access_action ("Common/nudge-playhead-backward");
2269                         }
2270                         break;
2271                 case MARKER:
2272                         text_message (path, "Marker", get_address (msg));
2273                         if (delta > 0) {
2274                                 next_marker ();
2275                         } else if (delta < 0) {
2276                                 prev_marker ();
2277                         }
2278                         break;
2279                 default:
2280                         break;
2281
2282         }
2283         return 0;
2284
2285 }
2286
2287 int
2288 OSC::jog_mode (float mode, lo_message msg)
2289 {
2290         if (!session) return -1;
2291
2292         OSCSurface *s = get_surface(get_address (msg));
2293
2294         switch((uint32_t)mode)
2295         {
2296                 case JOG  :
2297                         s->jogmode = JOG;
2298                         break;
2299                 case SCRUB:
2300                         s->jogmode = SCRUB;
2301                         break;
2302                 case SHUTTLE:
2303                         s->jogmode = SHUTTLE;
2304                         break;
2305                 case SCROLL:
2306                         s->jogmode = SCROLL;
2307                         break;
2308                 case TRACK:
2309                         s->jogmode = TRACK;
2310                         break;
2311                 case BANK:
2312                         s->jogmode = BANK;
2313                         break;
2314                 case NUDGE:
2315                         s->jogmode = NUDGE;
2316                         break;
2317                 case MARKER:
2318                         s->jogmode = MARKER;
2319                         break;
2320                 default:
2321                         PBD::warning << "Jog Mode: " << mode << " is not valid." << endmsg;
2322                         break;
2323         lo_message reply = lo_message_new ();
2324         lo_message_add_int32 (reply, s->jogmode);
2325         lo_send_message (get_address(msg), "/jog/mode", reply);
2326         lo_message_free (reply);
2327
2328         }
2329         jog (0, msg);
2330         return 0;
2331
2332 }
2333
2334 // master and monitor calls
2335 int
2336 OSC::master_set_gain (float dB)
2337 {
2338         if (!session) return -1;
2339         boost::shared_ptr<Stripable> s = session->master_out();
2340         if (s) {
2341                 if (dB < -192) {
2342                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2343                 } else {
2344                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2345                 }
2346         }
2347         return 0;
2348 }
2349
2350 int
2351 OSC::master_set_fader (float position)
2352 {
2353         if (!session) return -1;
2354         boost::shared_ptr<Stripable> s = session->master_out();
2355         if (s) {
2356                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2357         }
2358         return 0;
2359 }
2360
2361 int
2362 OSC::master_set_trim (float dB)
2363 {
2364         if (!session) return -1;
2365         boost::shared_ptr<Stripable> s = session->master_out();
2366
2367         if (s) {
2368                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2369         }
2370
2371         return 0;
2372 }
2373
2374 int
2375 OSC::master_set_pan_stereo_position (float position, lo_message msg)
2376 {
2377         if (!session) return -1;
2378         OSCSurface *sur = get_surface(get_address (msg));
2379
2380         float endposition = .5;
2381         boost::shared_ptr<Stripable> s = session->master_out();
2382
2383         if (s) {
2384                 if (s->pan_azimuth_control()) {
2385                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2386                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
2387                 }
2388         }
2389
2390         if (sur->feedback[4]) {
2391                 lo_message reply = lo_message_new ();
2392                 lo_message_add_float (reply, endposition);
2393
2394                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
2395                 lo_message_free (reply);
2396         }
2397
2398         return 0;
2399 }
2400
2401 int
2402 OSC::master_set_mute (uint32_t state)
2403 {
2404         if (!session) return -1;
2405
2406         boost::shared_ptr<Stripable> s = session->master_out();
2407
2408         if (s) {
2409                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
2410         }
2411
2412         return 0;
2413 }
2414
2415 int
2416 OSC::monitor_set_gain (float dB)
2417 {
2418         if (!session) return -1;
2419         boost::shared_ptr<Stripable> s = session->monitor_out();
2420
2421         if (s) {
2422                 if (dB < -192) {
2423                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2424                 } else {
2425                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2426                 }
2427         }
2428         return 0;
2429 }
2430
2431 int
2432 OSC::monitor_set_fader (float position)
2433 {
2434         if (!session) return -1;
2435         boost::shared_ptr<Stripable> s = session->monitor_out();
2436         if (s) {
2437                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
2438         }
2439         return 0;
2440 }
2441
2442 int
2443 OSC::monitor_set_mute (uint32_t state)
2444 {
2445         if (!session) return -1;
2446
2447         if (session->monitor_out()) {
2448                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2449                 mon->set_cut_all (state);
2450         }
2451         return 0;
2452 }
2453
2454 int
2455 OSC::monitor_set_dim (uint32_t state)
2456 {
2457         if (!session) return -1;
2458
2459         if (session->monitor_out()) {
2460                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2461                 mon->set_dim_all (state);
2462         }
2463         return 0;
2464 }
2465
2466 int
2467 OSC::monitor_set_mono (uint32_t state)
2468 {
2469         if (!session) return -1;
2470
2471         if (session->monitor_out()) {
2472                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2473                 mon->set_mono (state);
2474         }
2475         return 0;
2476 }
2477
2478 int
2479 OSC::route_get_sends(lo_message msg) {
2480         if (!session) {
2481                 return -1;
2482         }
2483
2484         lo_arg **argv = lo_message_get_argv(msg);
2485
2486         int rid = argv[0]->i;
2487
2488         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2489         if (!strip) {
2490                 return -1;
2491         }
2492
2493         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2494         if (!r) {
2495                 return -1;
2496         }
2497
2498         lo_message reply = lo_message_new();
2499         lo_message_add_int32(reply, rid);
2500
2501         int i = 0;
2502         for (;;) {
2503                 boost::shared_ptr<Processor> p = r->nth_send(i++);
2504
2505                 if (!p) {
2506                         break;
2507                 }
2508
2509                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2510                 if (isend) {
2511                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
2512                         lo_message_add_string(reply, isend->name().c_str());
2513                         lo_message_add_int32(reply, i);
2514                         boost::shared_ptr<Amp> a = isend->amp();
2515                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
2516                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2517                 }
2518         }
2519         // if used dedicated message path to identify this reply in async operation.
2520         // Naming it #reply wont help the client to identify the content.
2521         lo_send_message(get_address (msg), "/strip/sends", reply);
2522
2523         lo_message_free(reply);
2524
2525         return 0;
2526 }
2527
2528 int
2529 OSC::route_get_receives(lo_message msg) {
2530         if (!session) {
2531                 return -1;
2532         }
2533
2534         lo_arg **argv = lo_message_get_argv(msg);
2535
2536         uint32_t rid = argv[0]->i;
2537
2538
2539         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2540         if (!strip) {
2541                 return -1;
2542         }
2543
2544         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2545         if (!r) {
2546                 return -1;
2547         }
2548
2549         boost::shared_ptr<RouteList> route_list = session->get_routes();
2550
2551         lo_message reply = lo_message_new();
2552
2553         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
2554                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
2555                 if (!tr) {
2556                         continue;
2557                 }
2558                 int j = 0;
2559
2560                 for (;;) {
2561                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
2562
2563                         if (!p) {
2564                                 break;
2565                         }
2566
2567                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2568                         if (isend) {
2569                                 if( isend->target_route()->id() == r->id()){
2570                                         boost::shared_ptr<Amp> a = isend->amp();
2571
2572                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
2573                                         lo_message_add_string(reply, tr->name().c_str());
2574                                         lo_message_add_int32(reply, j);
2575                                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
2576                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2577                                 }
2578                         }
2579                 }
2580         }
2581
2582         // I have used a dedicated message path to identify this reply in async operation.
2583         // Naming it #reply wont help the client to identify the content.
2584         lo_send_message(get_address (msg), "/strip/receives", reply);
2585         lo_message_free(reply);
2586         return 0;
2587 }
2588
2589 // strip calls
2590
2591 int
2592 OSC::set_automation (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
2593 {
2594         if (!session) return -1;
2595
2596         int ret = 1;
2597         OSCSurface *sur = get_surface(get_address (msg));
2598         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2599         uint32_t ctr = 0;
2600         uint32_t aut = 0;
2601         uint32_t ssid;
2602
2603         if (argc) {
2604                 if (argv[argc - 1]->f) {
2605                         aut = (int)argv[argc - 1]->f;
2606                 } else {
2607                         aut = argv[argc - 1]->i;
2608                 }
2609         }
2610
2611         //parse path first to find stripable
2612         if (!strncmp (path, "/strip/", 7)) {
2613                 // find ssid and stripable
2614                 if (argc > 1) {
2615                         if (types[1] == 'f') {
2616                                 ssid = (uint32_t)argv[0]->f;
2617                         } else {
2618                                 ssid = argv[0]->i;
2619                         }
2620                         strp = get_strip (ssid, get_address (msg));
2621                 } else {
2622                         ssid = atoi (&(strrchr (path, '/' ))[1]);
2623                         strp = get_strip (ssid, get_address (msg));
2624                 }
2625                 ctr = 7;
2626         } else if (!strncmp (path, "/select/", 8)) {
2627                 if (sur->expand_enable && sur->expand) {
2628                         strp = get_strip (sur->expand, get_address (msg));
2629                 } else {
2630                         strp = ControlProtocol::first_selected_stripable();
2631                 }
2632                 ctr = 8;
2633         } else {
2634                 return ret;
2635         }
2636         if (strp) {
2637                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
2638                 // other automatable controls can be added by repeating the next 6.5 lines
2639                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2640                         if (strp->gain_control ()) {
2641                                 control = strp->gain_control ();
2642                         } else {
2643                                 PBD::warning << "No fader for this strip" << endmsg;
2644                         }
2645                 } else {
2646                         PBD::warning << "Automation not available for " << path << endmsg;
2647                 }
2648
2649                 if (control) {
2650
2651                         switch (aut) {
2652                                 case 0:
2653                                         control->set_automation_state (ARDOUR::Off);
2654                                         ret = 0;
2655                                         break;
2656                                 case 1:
2657                                         control->set_automation_state (ARDOUR::Play);
2658                                         ret = 0;
2659                                         break;
2660                                 case 2:
2661                                         control->set_automation_state (ARDOUR::Write);
2662                                         ret = 0;
2663                                         break;
2664                                 case 3:
2665                                         control->set_automation_state (ARDOUR::Touch);
2666                                         ret = 0;
2667                                         break;
2668                                 default:
2669                                         break;
2670                         }
2671                 }
2672         }
2673
2674         return ret;
2675 }
2676
2677 int
2678 OSC::touch_detect (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
2679 {
2680         if (!session) return -1;
2681
2682         int ret = 1;
2683         OSCSurface *sur = get_surface(get_address (msg));
2684         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2685         uint32_t ctr = 0;
2686         uint32_t touch = 0;
2687         uint32_t ssid;
2688
2689         if (argc) {
2690                 if (argv[argc - 1]->f) {
2691                         touch = (int)argv[argc - 1]->f;
2692                 } else {
2693                         touch = argv[argc - 1]->i;
2694                 }
2695         }
2696
2697         //parse path first to find stripable
2698         if (!strncmp (path, "/strip/", 7)) {
2699                 // find ssid and stripable
2700                 if (argc > 1) {
2701                         if (types[0] == 'f') {
2702                                 ssid = (uint32_t)argv[0]->f;
2703                         } else {
2704                                 ssid = argv[0]->i;
2705                         }
2706                         strp = get_strip (ssid, get_address (msg));
2707                 } else {
2708                         ssid = atoi (&(strrchr (path, '/' ))[1]);
2709                         strp = get_strip (ssid, get_address (msg));
2710                 }
2711                 ctr = 7;
2712         } else if (!strncmp (path, "/select/", 8)) {
2713                 if (sur->expand_enable && sur->expand) {
2714                         strp = get_strip (sur->expand, get_address (msg));
2715                 } else {
2716                         strp = ControlProtocol::first_selected_stripable();
2717                 }
2718                 ctr = 8;
2719         } else {
2720                 return ret;
2721         }
2722         if (strp) {
2723                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
2724                 // other automatable controls can be added by repeating the next 6.5 lines
2725                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2726                         if (strp->gain_control ()) {
2727                                 control = strp->gain_control ();
2728                         } else {
2729                                 PBD::warning << "No fader for this strip" << endmsg;
2730                         }
2731                 } else {
2732                         PBD::warning << "Automation not available for " << path << endmsg;
2733                 }
2734
2735                 if (control) {
2736                         if (touch) {
2737                                 //start touch
2738                                 if (control->automation_state() == Touch && !control->touching ()) {
2739                                         control->start_touch (control->session().transport_frame());
2740                                 }
2741                                 ret = 0;
2742                         } else {
2743                                 // end touch
2744                                 control->stop_touch (true, control->session().transport_frame());
2745                                 ret = 0;
2746                         }
2747                         // just in case some crazy surface starts sending control values before touch
2748                         FakeTouchMap::iterator x = _touch_timeout.find(control);
2749                         if (x != _touch_timeout.end()) {
2750                                 _touch_timeout.erase (x);
2751                         }
2752                 }
2753         }
2754
2755         return ret;
2756 }
2757
2758 int
2759 OSC::fake_touch (boost::shared_ptr<ARDOUR::AutomationControl> ctrl)
2760 {
2761         if (ctrl) {
2762                 //start touch
2763                 if (ctrl->automation_state() == Touch && !ctrl->touching ()) {
2764                 ctrl->start_touch (ctrl->session().transport_frame());
2765                 _touch_timeout[ctrl] = 10;
2766                 }
2767         }
2768
2769         return 0;
2770 }
2771
2772 int
2773 OSC::route_mute (int ssid, int yn, lo_message msg)
2774 {
2775         if (!session) return -1;
2776         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2777
2778         if (s) {
2779                 if (s->mute_control()) {
2780                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2781                         return 0;
2782                 }
2783         }
2784
2785         return route_send_fail ("mute", ssid, 0, get_address (msg));
2786 }
2787
2788 int
2789 OSC::sel_mute (uint32_t yn, lo_message msg)
2790 {
2791         OSCSurface *sur = get_surface(get_address (msg));
2792         boost::shared_ptr<Stripable> s;
2793         if (sur->expand_enable) {
2794                 s = get_strip (sur->expand, get_address (msg));
2795         } else {
2796                 s = _select;
2797         }
2798         if (s) {
2799                 if (s->mute_control()) {
2800                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2801                         return 0;
2802                 }
2803         }
2804         return sel_fail ("mute", 0, get_address (msg));
2805 }
2806
2807 int
2808 OSC::route_solo (int ssid, int yn, lo_message msg)
2809 {
2810         if (!session) return -1;
2811         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2812
2813         if (s) {
2814                 if (s->solo_control()) {
2815                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2816                 }
2817         }
2818
2819         return route_send_fail ("solo", ssid, 0, get_address (msg));
2820 }
2821
2822 int
2823 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2824 {
2825         if (!session) return -1;
2826         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2827
2828         if (s) {
2829                 if (s->solo_isolate_control()) {
2830                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2831                         return 0;
2832                 }
2833         }
2834
2835         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2836 }
2837
2838 int
2839 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2840 {
2841         if (!session) return -1;
2842         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2843
2844         if (s) {
2845                 if (s->solo_safe_control()) {
2846                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2847                         return 0;
2848                 }
2849         }
2850
2851         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2852 }
2853
2854 int
2855 OSC::sel_solo (uint32_t yn, lo_message msg)
2856 {
2857         OSCSurface *sur = get_surface(get_address (msg));
2858         boost::shared_ptr<Stripable> s;
2859         if (sur->expand_enable) {
2860                 s = get_strip (sur->expand, get_address (msg));
2861         } else {
2862                 s = _select;
2863         }
2864         if (s) {
2865                 if (s->solo_control()) {
2866                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2867                 }
2868         }
2869         return sel_fail ("solo", 0, get_address (msg));
2870 }
2871
2872 int
2873 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2874 {
2875         OSCSurface *sur = get_surface(get_address (msg));
2876         boost::shared_ptr<Stripable> s;
2877         if (sur->expand_enable) {
2878                 s = get_strip (sur->expand, get_address (msg));
2879         } else {
2880                 s = _select;
2881         }
2882         if (s) {
2883                 if (s->solo_isolate_control()) {
2884                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2885                         return 0;
2886                 }
2887         }
2888         return sel_fail ("solo_iso", 0, get_address (msg));
2889 }
2890
2891 int
2892 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2893 {
2894         OSCSurface *sur = get_surface(get_address (msg));
2895         boost::shared_ptr<Stripable> s;
2896         if (sur->expand_enable) {
2897                 s = get_strip (sur->expand, get_address (msg));
2898         } else {
2899                 s = _select;
2900         }
2901         if (s) {
2902                 if (s->solo_safe_control()) {
2903                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2904                         return 0;
2905                 }
2906         }
2907         return sel_fail ("solo_safe", 0, get_address (msg));
2908 }
2909
2910 int
2911 OSC::sel_recenable (uint32_t yn, lo_message msg)
2912 {
2913         OSCSurface *sur = get_surface(get_address (msg));
2914         boost::shared_ptr<Stripable> s;
2915         if (sur->expand_enable) {
2916                 s = get_strip (sur->expand, get_address (msg));
2917         } else {
2918                 s = _select;
2919         }
2920         if (s) {
2921                 if (s->rec_enable_control()) {
2922                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2923                         if (s->rec_enable_control()->get_value()) {
2924                                 return 0;
2925                         }
2926                 }
2927         }
2928         return sel_fail ("recenable", 0, get_address (msg));
2929 }
2930
2931 int
2932 OSC::route_recenable (int ssid, int yn, lo_message msg)
2933 {
2934         if (!session) return -1;
2935         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2936
2937         if (s) {
2938                 if (s->rec_enable_control()) {
2939                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2940                         if (s->rec_enable_control()->get_value()) {
2941                                 return 0;
2942                         }
2943                 }
2944         }
2945         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2946 }
2947
2948 int
2949 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2950     if (!session) {
2951         return -1;
2952     }
2953
2954     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2955
2956     if (s) {
2957         s->set_name(std::string(newname));
2958     }
2959
2960     return 0;
2961 }
2962
2963 int
2964 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2965 {
2966         OSCSurface *sur = get_surface(get_address (msg));
2967         boost::shared_ptr<Stripable> s;
2968         if (sur->expand_enable) {
2969                 s = get_strip (sur->expand, get_address (msg));
2970         } else {
2971                 s = _select;
2972         }
2973         if (s) {
2974                 if (s->rec_safe_control()) {
2975                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2976                         if (s->rec_safe_control()->get_value()) {
2977                                 return 0;
2978                         }
2979                 }
2980         }
2981         return sel_fail ("record_safe", 0, get_address (msg));
2982 }
2983
2984 int
2985 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2986 {
2987         if (!session) return -1;
2988         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2989         if (s) {
2990                 if (s->rec_safe_control()) {
2991                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2992                         if (s->rec_safe_control()->get_value()) {
2993                                 return 0;
2994                         }
2995                 }
2996         }
2997         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2998 }
2999
3000 int
3001 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
3002 {
3003         if (!session) return -1;
3004         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3005
3006         if (s) {
3007                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3008                 if (track) {
3009                         if (track->monitoring_control()) {
3010                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3011                                 return 0;
3012                         }
3013                 }
3014         }
3015
3016         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
3017 }
3018
3019 int
3020 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
3021 {
3022         OSCSurface *sur = get_surface(get_address (msg));
3023         boost::shared_ptr<Stripable> s;
3024         if (sur->expand_enable) {
3025                 s = get_strip (sur->expand, get_address (msg));
3026         } else {
3027                 s = _select;
3028         }
3029         if (s) {
3030                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3031                 if (track) {
3032                         if (track->monitoring_control()) {
3033                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3034                                 return 0;
3035                         }
3036                 }
3037         }
3038         return sel_fail ("monitor_input", 0, get_address (msg));
3039 }
3040
3041 int
3042 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
3043 {
3044         if (!session) return -1;
3045         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3046
3047         if (s) {
3048                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3049                 if (track) {
3050                         if (track->monitoring_control()) {
3051                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
3052                                 return 0;
3053                         }
3054                 }
3055         }
3056
3057         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
3058 }
3059
3060 int
3061 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
3062 {
3063         OSCSurface *sur = get_surface(get_address (msg));
3064         boost::shared_ptr<Stripable> s;
3065         if (sur->expand_enable) {
3066                 s = get_strip (sur->expand, get_address (msg));
3067         } else {
3068                 s = _select;
3069         }
3070         if (s) {
3071                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3072                 if (track) {
3073                         if (track->monitoring_control()) {
3074                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
3075                                 return 0;
3076                         }
3077                 }
3078         }
3079         return sel_fail ("monitor_disk", 0, get_address (msg));
3080 }
3081
3082
3083 int
3084 OSC::strip_phase (int ssid, int yn, lo_message msg)
3085 {
3086         if (!session) return -1;
3087         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3088
3089         if (s) {
3090                 if (s->phase_control()) {
3091                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3092                         return 0;
3093                 }
3094         }
3095
3096         return route_send_fail ("polarity", ssid, 0, get_address (msg));
3097 }
3098
3099 int
3100 OSC::sel_phase (uint32_t yn, lo_message msg)
3101 {
3102         OSCSurface *sur = get_surface(get_address (msg));
3103         boost::shared_ptr<Stripable> s;
3104         if (sur->expand_enable) {
3105                 s = get_strip (sur->expand, get_address (msg));
3106         } else {
3107                 s = _select;
3108         }
3109         if (s) {
3110                 if (s->phase_control()) {
3111                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3112                         return 0;
3113                 }
3114         }
3115         return sel_fail ("polarity", 0, get_address (msg));
3116 }
3117
3118 int
3119 OSC::strip_expand (int ssid, int yn, lo_message msg)
3120 {
3121         OSCSurface *sur = get_surface(get_address (msg));
3122         sur->expand_enable = (bool) yn;
3123         sur->expand = ssid;
3124         boost::shared_ptr<Stripable> s;
3125         if (yn) {
3126                 s = get_strip (ssid, get_address (msg));
3127         } else {
3128                 s = ControlProtocol::first_selected_stripable();
3129         }
3130
3131         return _strip_select (s, get_address (msg));
3132 }
3133
3134 int
3135 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
3136 {
3137         if (!session) {
3138                 return -1;
3139         }
3140         OSCSurface *sur = get_surface(addr);
3141         if (sur->sel_obs) {
3142                 delete sur->sel_obs;
3143                 sur->sel_obs = 0;
3144         }
3145         bool feedback_on = sur->feedback.to_ulong();
3146         if (s && feedback_on) {
3147                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur);
3148                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
3149                 sur->sel_obs = sel_fb;
3150         } else if (sur->expand_enable) {
3151                 // expand doesn't point to a stripable, turn it off and use select
3152                 sur->expand = 0;
3153                 sur->expand_enable = false;
3154                 if (_select && feedback_on) {
3155                         s = _select;
3156                         OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur);
3157                         s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
3158                         sur->sel_obs = sel_fb;
3159                 }
3160         } else if (feedback_on) {
3161                 route_send_fail ("select", sur->expand, 0 , addr);
3162         }
3163         // need to set monitor for processor changed signal
3164         // detecting processor changes requires cast to route
3165         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
3166         if (r) {
3167                 r->processors_changed.connect  (sur->proc_connection, MISSING_INVALIDATOR, boost::bind (&OSC::processor_changed, this, addr), this);
3168                 processor_changed (addr);
3169         }
3170
3171         if (!feedback_on) {
3172                 return 0;
3173         }
3174         //update buttons on surface
3175         int b_s = sur->bank_size;
3176         if (!b_s) { // bank size 0 means we need to know how many strips there are.
3177                 b_s = sur->nstrips;
3178         }
3179         for (int i = 1;  i <= b_s; i++) {
3180                 string path = "expand";
3181
3182                 if ((i == (int) sur->expand) && sur->expand_enable) {
3183                         lo_message reply = lo_message_new ();
3184                         if (sur->feedback[2]) {
3185                                 ostringstream os;
3186                                 os << "/strip/" << path << "/" << i;
3187                                 path = os.str();
3188                         } else {
3189                                 ostringstream os;
3190                                 os << "/strip/" << path;
3191                                 path = os.str();
3192                                 lo_message_add_int32 (reply, i);
3193                         }
3194                         lo_message_add_float (reply, (float) 1);
3195
3196                         lo_send_message (addr, path.c_str(), reply);
3197                         lo_message_free (reply);
3198                         reply = lo_message_new ();
3199                         lo_message_add_float (reply, 1.0);
3200                         lo_send_message (addr, "/select/expand", reply);
3201                         lo_message_free (reply);
3202
3203                 } else {
3204                         lo_message reply = lo_message_new ();
3205                         lo_message_add_int32 (reply, i);
3206                         lo_message_add_float (reply, 0.0);
3207                         lo_send_message (addr, "/strip/expand", reply);
3208                         lo_message_free (reply);
3209                 }
3210         }
3211         if (!sur->expand_enable) {
3212                 lo_message reply = lo_message_new ();
3213                 lo_message_add_float (reply, 0.0);
3214                 lo_send_message (addr, "/select/expand", reply);
3215                 lo_message_free (reply);
3216         }
3217
3218         return 0;
3219 }
3220
3221 void
3222 OSC::processor_changed (lo_address addr)
3223 {
3224         OSCSurface *sur = get_surface (addr);
3225         sur->proc_connection.disconnect ();
3226         _sel_plugin (sur->plugin_id, addr);
3227         if (sur->sel_obs) {
3228                 sur->sel_obs->renew_sends ();
3229                 sur->sel_obs->eq_restart (-1);
3230         }
3231 }
3232
3233 int
3234 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
3235 {
3236         //ignore button release
3237         if (!yn) return 0;
3238
3239         if (!session) {
3240                 return -1;
3241         }
3242         OSCSurface *sur = get_surface(get_address (msg));
3243         sur->expand_enable = false;
3244         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3245         if (s) {
3246                 SetStripableSelection (s);
3247         } else {
3248                 if ((int) (sur->feedback.to_ulong())) {
3249                         route_send_fail ("select", ssid, 0, get_address (msg));
3250                 }
3251         }
3252
3253         return 0;
3254 }
3255
3256 int
3257 OSC::sel_expand (uint32_t state, lo_message msg)
3258 {
3259         OSCSurface *sur = get_surface(get_address (msg));
3260         boost::shared_ptr<Stripable> s;
3261         sur->expand_enable = (bool) state;
3262         if (state && sur->expand) {
3263                 s = get_strip (sur->expand, get_address (msg));
3264         } else {
3265                 s = ControlProtocol::first_selected_stripable();
3266         }
3267
3268         return _strip_select (s, get_address (msg));
3269 }
3270
3271 int
3272 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
3273 {
3274         if (!session) return -1;
3275         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3276
3277         if (s) {
3278                 if (s->gain_control()) {
3279                         fake_touch (s->gain_control());
3280                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
3281                 } else {
3282                         return 1;
3283                 }
3284         } else {
3285                 return 1;
3286         }
3287
3288         return 0;
3289 }
3290
3291 int
3292 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
3293 {
3294         if (!session) {
3295                 route_send_fail ("gain", ssid, -193, get_address (msg));
3296                 return -1;
3297         }
3298         int ret;
3299         if (dB < -192) {
3300                 ret = route_set_gain_abs (ssid, 0.0, msg);
3301         } else {
3302                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
3303         }
3304         if (ret != 0) {
3305                 return route_send_fail ("gain", ssid, -193, get_address (msg));
3306         }
3307         return 0;
3308 }
3309
3310 int
3311 OSC::sel_gain (float val, lo_message msg)
3312 {
3313         OSCSurface *sur = get_surface(get_address (msg));
3314         boost::shared_ptr<Stripable> s;
3315         if (sur->expand_enable) {
3316                 s = get_strip (sur->expand, get_address (msg));
3317         } else {
3318                 s = _select;
3319         }
3320         if (s) {
3321                 float abs;
3322                 if (val < -192) {
3323                         abs = 0;
3324                 } else {
3325                         abs = dB_to_coefficient (val);
3326                 }
3327                 if (s->gain_control()) {
3328                         fake_touch (s->gain_control());
3329                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3330                         return 0;
3331                 }
3332         }
3333         return sel_fail ("gain", -193, get_address (msg));
3334 }
3335
3336 int
3337 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
3338 {
3339         if (!session) {
3340                 return -1;
3341         }
3342         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3343
3344         if (s) {
3345                 if (s->gain_control()) {
3346                         fake_touch (s->gain_control());
3347                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
3348                 } else {
3349                         return route_send_fail ("fader", ssid, 0, get_address (msg));
3350                 }
3351         } else {
3352                 return route_send_fail ("fader", ssid, 0, get_address (msg));
3353         }
3354         return 0;
3355 }
3356
3357 int
3358 OSC::strip_db_delta (int ssid, float delta, lo_message msg)
3359 {
3360         if (!session) return -1;
3361         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3362         if (s) {
3363                 float db = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
3364                 float abs;
3365                 if (db < -192) {
3366                         abs = 0;
3367                 } else {
3368                         abs = dB_to_coefficient (db);
3369                 }
3370                 s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3371                 return 0;
3372         }
3373         return -1;
3374 }
3375
3376 int
3377 OSC::sel_fader (float val, lo_message msg)
3378 {
3379         OSCSurface *sur = get_surface(get_address (msg));
3380         boost::shared_ptr<Stripable> s;
3381         if (sur->expand_enable) {
3382                 s = get_strip (sur->expand, get_address (msg));
3383         } else {
3384                 s = _select;
3385         }
3386         if (s) {
3387                 if (s->gain_control()) {
3388                         fake_touch (s->gain_control());
3389                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3390                         return 0;
3391                 }
3392         }
3393         return sel_fail ("fader", 0, get_address (msg));
3394 }
3395
3396 int
3397 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
3398 {
3399         if (!session) return -1;
3400         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3401
3402         if (s) {
3403                 if (s->trim_control()) {
3404                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
3405                         return 0;
3406                 }
3407
3408         }
3409
3410         return -1;
3411 }
3412
3413 int
3414 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
3415 {
3416         int ret;
3417         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
3418         if (ret != 0) {
3419                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
3420         }
3421
3422 return 0;
3423 }
3424
3425 int
3426 OSC::sel_trim (float val, lo_message msg)
3427 {
3428         OSCSurface *sur = get_surface(get_address (msg));
3429         boost::shared_ptr<Stripable> s;
3430         if (sur->expand_enable) {
3431                 s = get_strip (sur->expand, get_address (msg));
3432         } else {
3433                 s = _select;
3434         }
3435         if (s) {
3436                 if (s->trim_control()) {
3437                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
3438                         return 0;
3439                 }
3440         }
3441         return sel_fail ("trimdB", 0, get_address (msg));
3442 }
3443
3444 int
3445 OSC::sel_pan_position (float val, lo_message msg)
3446 {
3447         OSCSurface *sur = get_surface(get_address (msg));
3448         boost::shared_ptr<Stripable> s;
3449         if (sur->expand_enable) {
3450                 s = get_strip (sur->expand, get_address (msg));
3451         } else {
3452                 s = _select;
3453         }
3454         if (s) {
3455                 if(s->pan_azimuth_control()) {
3456                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3457                         return 0;
3458                 }
3459         }
3460         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
3461 }
3462
3463 int
3464 OSC::sel_pan_width (float val, lo_message msg)
3465 {
3466         OSCSurface *sur = get_surface(get_address (msg));
3467         boost::shared_ptr<Stripable> s;
3468         if (sur->expand_enable) {
3469                 s = get_strip (sur->expand, get_address (msg));
3470         } else {
3471                 s = _select;
3472         }
3473         if (s) {
3474                 if (s->pan_width_control()) {
3475                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3476                         return 0;
3477                 }
3478         }
3479         return sel_fail ("pan_stereo_width", 1, get_address (msg));
3480 }
3481
3482 int
3483 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
3484 {
3485         if (!session) return -1;
3486         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3487
3488         if (s) {
3489                 if(s->pan_azimuth_control()) {
3490                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
3491                         return 0;
3492                 }
3493         }
3494
3495         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
3496 }
3497
3498 int
3499 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
3500 {
3501         if (!session) return -1;
3502         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3503
3504         if (s) {
3505                 if (s->pan_width_control()) {
3506                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
3507                         return 0;
3508                 }
3509         }
3510
3511         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
3512 }
3513
3514 int
3515 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
3516 {
3517         if (!session) {
3518                 return -1;
3519         }
3520         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3521         float abs;
3522         if (s) {
3523                 if (id > 0) {
3524                         --id;
3525                 }
3526 #ifdef MIXBUS
3527                 abs = val;
3528 #else
3529                 if (val < -192) {
3530                         abs = 0;
3531                 } else {
3532                         abs = dB_to_coefficient (val);
3533                 }
3534 #endif
3535                 if (s->send_level_controllable (id)) {
3536                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3537                         return 0;
3538                 }
3539         }
3540         return 0;
3541 }
3542
3543 int
3544 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
3545 {
3546         if (!session) {
3547                 return -1;
3548         }
3549         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3550         float abs;
3551         if (s) {
3552
3553                 if (id > 0) {
3554                         --id;
3555                 }
3556
3557                 if (s->send_level_controllable (id)) {
3558                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3559                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3560                         return 0;
3561                 }
3562         }
3563         return 0;
3564 }
3565
3566 int
3567 OSC::sel_sendgain (int id, float val, lo_message msg)
3568 {
3569         OSCSurface *sur = get_surface(get_address (msg));
3570         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3571                 return sel_send_fail ("send_gain", id, -193, get_address (msg));
3572         }
3573         boost::shared_ptr<Stripable> s;
3574         if (sur->expand_enable) {
3575                 s = get_strip (sur->expand, get_address (msg));
3576         } else {
3577                 s = _select;
3578         }
3579         float abs;
3580         int send_id;
3581         if (s) {
3582                 if (id > 0) {
3583                         send_id = id - 1;
3584                 }
3585 #ifdef MIXBUS
3586                 abs = val;
3587 #else
3588                 if (val < -192) {
3589                         abs = 0;
3590                 } else {
3591                         abs = dB_to_coefficient (val);
3592                 }
3593 #endif
3594                 if (sur->send_page_size) {
3595                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3596                 }
3597                 if (s->send_level_controllable (send_id)) {
3598                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
3599                         return 0;
3600                 }
3601         }
3602         return sel_send_fail ("send_gain", id, -193, get_address (msg));
3603 }
3604
3605 int
3606 OSC::sel_sendfader (int id, float val, lo_message msg)
3607 {
3608         OSCSurface *sur = get_surface(get_address (msg));
3609         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3610                 return sel_send_fail ("send_fader", id, 0, get_address (msg));
3611         }
3612         boost::shared_ptr<Stripable> s;
3613         if (sur->expand_enable) {
3614                 s = get_strip (sur->expand, get_address (msg));
3615         } else {
3616                 s = _select;
3617         }
3618         float abs;
3619         int send_id;
3620         if (s) {
3621
3622                 if (id > 0) {
3623                         send_id = id - 1;
3624                 }
3625                 if (sur->send_page_size) {
3626                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3627                 }
3628
3629                 if (s->send_level_controllable (send_id)) {
3630                         abs = s->send_level_controllable(send_id)->interface_to_internal (val);
3631                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
3632                         return 0;
3633                 }
3634         }
3635         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3636 }
3637
3638 int
3639 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3640 {
3641         if (!session) {
3642                 return -1;
3643         }
3644         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3645
3646         if (s) {
3647
3648                 /* revert to zero-based counting */
3649
3650                 if (sid > 0) {
3651                         --sid;
3652                 }
3653
3654                 if (s->send_enable_controllable (sid)) {
3655                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
3656                         return 0;
3657                 }
3658
3659                 if (s->send_level_controllable (sid)) {
3660                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3661                         if (!r) {
3662                                 return 0;
3663                         }
3664                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3665                         if (snd) {
3666                                 if (val) {
3667                                         snd->activate();
3668                                 } else {
3669                                         snd->deactivate();
3670                                 }
3671                         }
3672                         return 0;
3673                 }
3674
3675         }
3676
3677         return -1;
3678 }
3679
3680 int
3681 OSC::sel_sendenable (int id, float val, lo_message msg)
3682 {
3683         OSCSurface *sur = get_surface(get_address (msg));
3684         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
3685                 return sel_send_fail ("send_enable", id, 0, get_address (msg));
3686         }
3687         boost::shared_ptr<Stripable> s;
3688         if (sur->expand_enable) {
3689                 s = get_strip (sur->expand, get_address (msg));
3690         } else {
3691                 s = _select;
3692         }
3693         int send_id;
3694         if (s) {
3695                 if (id > 0) {
3696                         send_id = id - 1;
3697                 }
3698                 if (sur->send_page_size) {
3699                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
3700                 }
3701                 if (s->send_enable_controllable (send_id)) {
3702                         s->send_enable_controllable (send_id)->set_value (val, PBD::Controllable::NoGroup);
3703                         return 0;
3704                 }
3705                 if (s->send_level_controllable (send_id)) {
3706                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3707                         if (!r) {
3708                                 // should never get here
3709                                 return sel_send_fail ("send_enable", id, 0, get_address (msg));
3710                         }
3711                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(send_id));
3712                         if (snd) {
3713                                 if (val) {
3714                                         snd->activate();
3715                                 } else {
3716                                         snd->deactivate();
3717                                 }
3718                         }
3719                         return 0;
3720                 }
3721         }
3722         return sel_send_fail ("send_enable", id, 0, get_address (msg));
3723 }
3724
3725 int
3726 OSC::sel_master_send_enable (int state, lo_message msg)
3727 {
3728         OSCSurface *sur = get_surface(get_address (msg));
3729         boost::shared_ptr<Stripable> s;
3730         if (sur->expand_enable) {
3731                 s = get_strip (sur->expand, get_address (msg));
3732         } else {
3733                 s = _select;
3734         }
3735         if (s) {
3736                 if (s->master_send_enable_controllable ()) {
3737                         s->master_send_enable_controllable()->set_value (state, PBD::Controllable::NoGroup);
3738                         return 0;
3739                 }
3740         }
3741         return cue_float_message ("/select/master_send_enable", 0, get_address(msg));
3742 }
3743
3744 int
3745 OSC::select_plugin_parameter (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg) {
3746         OSCSurface *sur = get_surface(get_address (msg));
3747         int paid;
3748         int piid = sur->plugin_id;
3749         float value = 0;
3750         if (argc > 1) {
3751                 // no inline args
3752                 if (argc == 2) {
3753                         // change parameter in already selected plugin
3754                         if (argv[0]->f) {
3755                                 paid = (int) argv[0]->f;
3756                         } else {
3757                                 paid = argv[0]->i;
3758                         }
3759                         value = argv[1]->f;
3760                 } else if (argc == 3) {
3761                         if (argv[0]->f) {
3762                                 piid = (int) argv[0]->f;
3763                         } else {
3764                                 piid = argv[0]->i;
3765                         }
3766                         _sel_plugin (piid, get_address (msg));
3767                         if (argv[1]->f) {
3768                                 paid = (int) argv[1]->f;
3769                         } else {
3770                                 paid = argv[1]->i;
3771                         }
3772                         value = argv[2]->f;
3773                 } else if (argc > 3) {
3774                         PBD::warning << "OSC: Too many parameters: " << argc << endmsg;
3775                         return -1;
3776                 }
3777         } else if (argc) {
3778                 const char * par = strstr (&path[25], "/");
3779                 if (par) {
3780                         piid = atoi (&path[25]);
3781                         _sel_plugin (piid, msg);
3782                         paid = atoi (&par[1]);
3783                         value = argv[0]->f;
3784                         // we have plugin id too
3785                 } else {
3786                         // just parameter
3787                         paid = atoi (&path[25]);
3788                         value = argv[0]->f;
3789                 }
3790         } else {
3791                 PBD::warning << "OSC: Must have parameters." << endmsg;
3792                 return -1;
3793         }
3794         if (piid != sur->plugin_id) {
3795                 // if the user is sending to a non-existant plugin, don't adjust one we do have
3796                 PBD::warning << "OSC: plugin: " << piid << " out of range" << endmsg;
3797                 return -1;
3798         }
3799         if (sur->plug_page_size && (paid > (int)sur->plug_page_size)) {
3800                 return sel_send_fail ("plugin/parameter", paid, 0, get_address (msg));
3801         }
3802         boost::shared_ptr<Stripable> s;
3803         if (sur->expand_enable) {
3804                 s = get_strip (sur->expand, get_address (msg));
3805         } else {
3806                 s = _select;
3807         }
3808         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
3809         if (!r) {
3810                 return 1;
3811         }
3812
3813         boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
3814         boost::shared_ptr<PluginInsert> pi;
3815         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
3816                 return 1;
3817         }
3818         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3819         // paid is paged parameter convert to absolute
3820         int parid = paid + (int)(sur->plug_page_size * (sur->plug_page - 1));
3821         if (parid > (int) sur->plug_params.size ()) {
3822                 if (sur->feedback[13]) {
3823                         sel_send_fail ("plugin/parameter", paid, 0, get_address (msg));
3824                 }
3825                 return 0;
3826         }
3827
3828         bool ok = false;
3829         uint32_t controlid = pip->nth_parameter(sur->plug_params[parid - 1], ok);
3830         if (!ok) {
3831                 return 1;
3832         }
3833         ParameterDescriptor pd;
3834         pip->get_parameter_descriptor(controlid, pd);
3835         if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3836                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3837                 if (c) {
3838                         if (pd.integer_step && pd.upper == 1) {
3839                                 if (c->get_value () && value < 1.0) {
3840                                         c->set_value (0, PBD::Controllable::NoGroup);
3841                                 } else if (!c->get_value () && value) {
3842                                         c->set_value (1, PBD::Controllable::NoGroup);
3843                                 }
3844                         } else {
3845                                 c->set_value (c->interface_to_internal (value), PBD::Controllable::NoGroup);
3846                         }
3847                         return 0;
3848                 }
3849         }
3850         return 1;
3851 }
3852
3853 int
3854 OSC::route_plugin_list (int ssid, lo_message msg) {
3855         if (!session) {
3856                 return -1;
3857         }
3858
3859         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3860
3861         if (!r) {
3862                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3863                 return -1;
3864         }
3865         int piid = 0;
3866
3867         lo_message reply = lo_message_new ();
3868         lo_message_add_int32 (reply, ssid);
3869
3870
3871         for (;;) {
3872                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
3873                 if ( !redi ) {
3874                         break;
3875                 }
3876
3877                 boost::shared_ptr<PluginInsert> pi;
3878
3879                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3880                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3881                         continue;
3882                 }
3883                 lo_message_add_int32 (reply, piid + 1);
3884
3885                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3886                 lo_message_add_string (reply, pip->name());
3887
3888                 piid++;
3889         }
3890
3891         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
3892         lo_message_free (reply);
3893         return 0;
3894 }
3895
3896 int
3897 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
3898         if (!session) {
3899                 return -1;
3900         }
3901
3902         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3903
3904         if (!r) {
3905                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3906                 return -1;
3907         }
3908
3909         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3910
3911         if (!redi) {
3912                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3913                 return -1;
3914         }
3915
3916         boost::shared_ptr<PluginInsert> pi;
3917
3918         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3919                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3920                 return -1;
3921         }
3922
3923         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3924         bool ok = false;
3925
3926         lo_message reply = lo_message_new();
3927         lo_message_add_int32 (reply, ssid);
3928         lo_message_add_int32 (reply, piid);
3929         lo_message_add_string (reply, pip->name());
3930         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
3931
3932                 uint32_t controlid = pip->nth_parameter(ppi, ok);
3933                 if (!ok) {
3934                         continue;
3935                 }
3936                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3937                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3938
3939                                 lo_message_add_int32 (reply, ppi + 1);
3940                                 ParameterDescriptor pd;
3941                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
3942                                 lo_message_add_string (reply, pd.label.c_str());
3943
3944                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
3945                                 int flags = 0;
3946                                 flags |= pd.enumeration ? 1 : 0;
3947                                 flags |= pd.integer_step ? 2 : 0;
3948                                 flags |= pd.logarithmic ? 4 : 0;
3949                                 flags |= pd.sr_dependent ? 32 : 0;
3950                                 flags |= pd.toggled ? 64 : 0;
3951                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
3952                                 lo_message_add_int32 (reply, flags);
3953
3954                                 lo_message_add_int32 (reply, pd.datatype);
3955                                 lo_message_add_float (reply, pd.lower);
3956                                 lo_message_add_float (reply, pd.upper);
3957                                 lo_message_add_string (reply, pd.print_fmt.c_str());
3958                                 if ( pd.scale_points ) {
3959                                         lo_message_add_int32 (reply, pd.scale_points->size());
3960                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
3961                                                 lo_message_add_int32 (reply, i->second);
3962                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
3963                                         }
3964                                 }
3965                                 else {
3966                                         lo_message_add_int32 (reply, 0);
3967                                 }
3968                                 if ( c ) {
3969                                         lo_message_add_double (reply, c->get_value());
3970                                 }
3971                                 else {
3972                                         lo_message_add_double (reply, 0);
3973                         }
3974                 }
3975         }
3976
3977         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3978         lo_message_free (reply);
3979
3980         return 0;
3981 }
3982
3983 int
3984 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3985         if (!session) {
3986                 return -1;
3987         }
3988
3989         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3990
3991         if (!r) {
3992                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3993                 return -1;
3994         }
3995
3996         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3997
3998         if (!redi) {
3999                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4000                 return -1;
4001         }
4002
4003         boost::shared_ptr<PluginInsert> pi;
4004
4005         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4006                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4007                 return -1;
4008         }
4009
4010         pi->reset_parameters_to_default ();
4011
4012         return 0;
4013 }
4014
4015 int
4016 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
4017 {
4018         if (!session)
4019                 return -1;
4020         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4021
4022         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4023
4024         if (!r) {
4025                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4026                 return -1;
4027         }
4028
4029         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4030
4031         if (!redi) {
4032                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4033                 return -1;
4034         }
4035
4036         boost::shared_ptr<PluginInsert> pi;
4037
4038         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4039                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4040                 return -1;
4041         }
4042
4043         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4044         bool ok=false;
4045
4046         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4047
4048         if (!ok) {
4049                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
4050                 return -1;
4051         }
4052
4053         if (!pip->parameter_is_input(controlid)) {
4054                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
4055                 return -1;
4056         }
4057
4058         ParameterDescriptor pd;
4059         pi->plugin()->get_parameter_descriptor (controlid,pd);
4060
4061         if (val >= pd.lower && val <= pd.upper) {
4062
4063                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4064                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
4065                 c->set_value (val, PBD::Controllable::NoGroup);
4066         } else {
4067                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
4068                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
4069         }
4070
4071         return 0;
4072 }
4073
4074 //prints to cerr only
4075 int
4076 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
4077 {
4078         if (!session) {
4079                 return -1;
4080         }
4081         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4082
4083         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4084
4085         if (!r) {
4086                 return -1;
4087         }
4088
4089         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4090
4091         if (!redi) {
4092                 return -1;
4093         }
4094
4095         boost::shared_ptr<PluginInsert> pi;
4096
4097         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4098                 return -1;
4099         }
4100
4101         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4102         bool ok=false;
4103
4104         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4105
4106         if (!ok) {
4107                 return -1;
4108         }
4109
4110         ParameterDescriptor pd;
4111
4112         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
4113                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4114
4115                 cerr << "parameter:     " << pd.label  << "\n";
4116                 if (c) {
4117                         cerr << "current value: " << c->get_value () << "\n";
4118                 } else {
4119                         cerr << "current value not available, control does not exist\n";
4120                 }
4121                 cerr << "lower value:   " << pd.lower << "\n";
4122                 cerr << "upper value:   " << pd.upper << "\n";
4123         }
4124
4125         return 0;
4126 }
4127
4128 int
4129 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
4130 {
4131         if (!session)
4132                 return -1;
4133         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4134
4135         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4136
4137         if (!r) {
4138                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4139                 return -1;
4140         }
4141
4142         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4143
4144         if (!redi) {
4145                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4146                 return -1;
4147         }
4148
4149         boost::shared_ptr<PluginInsert> pi;
4150
4151         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4152                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4153                 return -1;
4154         }
4155
4156         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4157         pi->activate();
4158
4159         return 0;
4160 }
4161
4162 int
4163 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
4164 {
4165         if (!session)
4166                 return -1;
4167         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
4168
4169         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4170
4171         if (!r) {
4172                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4173                 return -1;
4174         }
4175
4176         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4177
4178         if (!redi) {
4179                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4180                 return -1;
4181         }
4182
4183         boost::shared_ptr<PluginInsert> pi;
4184
4185         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4186                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4187                 return -1;
4188         }
4189
4190         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4191         pi->deactivate();
4192
4193         return 0;
4194 }
4195
4196 // select
4197
4198 int
4199 OSC::sel_pan_elevation (float val, lo_message msg)
4200 {
4201         OSCSurface *sur = get_surface(get_address (msg));
4202         boost::shared_ptr<Stripable> s;
4203         if (sur->expand_enable) {
4204                 s = get_strip (sur->expand, get_address (msg));
4205         } else {
4206                 s = _select;
4207         }
4208         if (s) {
4209                 if (s->pan_elevation_control()) {
4210                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4211                         return 0;
4212                 }
4213         }
4214         return sel_fail ("pan_elevation_position", 0, get_address (msg));
4215 }
4216
4217 int
4218 OSC::sel_pan_frontback (float val, lo_message msg)
4219 {
4220         OSCSurface *sur = get_surface(get_address (msg));
4221         boost::shared_ptr<Stripable> s;
4222         if (sur->expand_enable) {
4223                 s = get_strip (sur->expand, get_address (msg));
4224         } else {
4225                 s = _select;
4226         }
4227         if (s) {
4228                 if (s->pan_frontback_control()) {
4229                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4230                         return 0;
4231                 }
4232         }
4233         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
4234 }
4235
4236 int
4237 OSC::sel_pan_lfe (float val, lo_message msg)
4238 {
4239         OSCSurface *sur = get_surface(get_address (msg));
4240         boost::shared_ptr<Stripable> s;
4241         if (sur->expand_enable) {
4242                 s = get_strip (sur->expand, get_address (msg));
4243         } else {
4244                 s = _select;
4245         }
4246         if (s) {
4247                 if (s->pan_lfe_control()) {
4248                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4249                         return 0;
4250                 }
4251         }
4252         return sel_fail ("pan_lfe_control", 0, get_address (msg));
4253 }
4254
4255 // compressor control
4256 int
4257 OSC::sel_comp_enable (float val, lo_message msg)
4258 {
4259         OSCSurface *sur = get_surface(get_address (msg));
4260         boost::shared_ptr<Stripable> s;
4261         if (sur->expand_enable) {
4262                 s = get_strip (sur->expand, get_address (msg));
4263         } else {
4264                 s = _select;
4265         }
4266         if (s) {
4267                 if (s->comp_enable_controllable()) {
4268                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4269                         return 0;
4270                 }
4271         }
4272         return sel_fail ("comp_enable", 0, get_address (msg));
4273 }
4274
4275 int
4276 OSC::sel_comp_threshold (float val, lo_message msg)
4277 {
4278         OSCSurface *sur = get_surface(get_address (msg));
4279         boost::shared_ptr<Stripable> s;
4280         if (sur->expand_enable) {
4281                 s = get_strip (sur->expand, get_address (msg));
4282         } else {
4283                 s = _select;
4284         }
4285         if (s) {
4286                 if (s->comp_threshold_controllable()) {
4287                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4288                         return 0;
4289                 }
4290         }
4291         return sel_fail ("comp_threshold", 0, get_address (msg));
4292 }
4293
4294 int
4295 OSC::sel_comp_speed (float val, lo_message msg)
4296 {
4297         OSCSurface *sur = get_surface(get_address (msg));
4298         boost::shared_ptr<Stripable> s;
4299         if (sur->expand_enable) {
4300                 s = get_strip (sur->expand, get_address (msg));
4301         } else {
4302                 s = _select;
4303         }
4304         if (s) {
4305                 if (s->comp_speed_controllable()) {
4306                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4307                         return 0;
4308                 }
4309         }
4310         return sel_fail ("comp_speed", 0, get_address (msg));
4311 }
4312
4313 int
4314 OSC::sel_comp_mode (float val, lo_message msg)
4315 {
4316         OSCSurface *sur = get_surface(get_address (msg));
4317         boost::shared_ptr<Stripable> s;
4318         if (sur->expand_enable) {
4319                 s = get_strip (sur->expand, get_address (msg));
4320         } else {
4321                 s = _select;
4322         }
4323         if (s) {
4324                 if (s->comp_mode_controllable()) {
4325                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4326                         return 0;
4327                 }
4328         }
4329         return sel_fail ("comp_mode", 0, get_address (msg));
4330 }
4331
4332 int
4333 OSC::sel_comp_makeup (float val, lo_message msg)
4334 {
4335         OSCSurface *sur = get_surface(get_address (msg));
4336         boost::shared_ptr<Stripable> s;
4337         if (sur->expand_enable) {
4338                 s = get_strip (sur->expand, get_address (msg));
4339         } else {
4340                 s = _select;
4341         }
4342         if (s) {
4343                 if (s->comp_makeup_controllable()) {
4344                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4345                         return 0;
4346                 }
4347         }
4348         return sel_fail ("comp_makeup", 0, get_address (msg));
4349 }
4350
4351 // EQ control
4352
4353 int
4354 OSC::sel_eq_enable (float val, lo_message msg)
4355 {
4356         OSCSurface *sur = get_surface(get_address (msg));
4357         boost::shared_ptr<Stripable> s;
4358         if (sur->expand_enable) {
4359                 s = get_strip (sur->expand, get_address (msg));
4360         } else {
4361                 s = _select;
4362         }
4363         if (s) {
4364                 if (s->eq_enable_controllable()) {
4365                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
4366                         return 0;
4367                 }
4368         }
4369         return sel_fail ("eq_enable", 0, get_address (msg));
4370 }
4371
4372 int
4373 OSC::sel_eq_hpf_freq (float val, lo_message msg)
4374 {
4375         OSCSurface *sur = get_surface(get_address (msg));
4376         boost::shared_ptr<Stripable> s;
4377         if (sur->expand_enable) {
4378                 s = get_strip (sur->expand, get_address (msg));
4379         } else {
4380                 s = _select;
4381         }
4382         if (s) {
4383                 if (s->filter_freq_controllable(true)) {
4384                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4385                         return 0;
4386                 }
4387         }
4388         return sel_fail ("eq_hpf/freq", 0, get_address (msg));
4389 }
4390
4391 int
4392 OSC::sel_eq_lpf_freq (float val, lo_message msg)
4393 {
4394         OSCSurface *sur = get_surface(get_address (msg));
4395         boost::shared_ptr<Stripable> s;
4396         if (sur->expand_enable) {
4397                 s = get_strip (sur->expand, get_address (msg));
4398         } else {
4399                 s = _select;
4400         }
4401         if (s) {
4402                 if (s->filter_freq_controllable(false)) {
4403                         s->filter_freq_controllable(false)->set_value (s->filter_freq_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4404                         return 0;
4405                 }
4406         }
4407         return sel_fail ("eq_lpf/freq", 0, get_address (msg));
4408 }
4409
4410 int
4411 OSC::sel_eq_hpf_enable (float val, lo_message msg)
4412 {
4413         OSCSurface *sur = get_surface(get_address (msg));
4414         boost::shared_ptr<Stripable> s;
4415         if (sur->expand_enable) {
4416                 s = get_strip (sur->expand, get_address (msg));
4417         } else {
4418                 s = _select;
4419         }
4420         if (s) {
4421                 if (s->filter_enable_controllable(true)) {
4422                         s->filter_enable_controllable(true)->set_value (s->filter_enable_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4423                         return 0;
4424                 }
4425         }
4426         return sel_fail ("eq_hpf/enable", 0, get_address (msg));
4427 }
4428
4429 int
4430 OSC::sel_eq_lpf_enable (float val, lo_message msg)
4431 {
4432         OSCSurface *sur = get_surface(get_address (msg));
4433         boost::shared_ptr<Stripable> s;
4434         if (sur->expand_enable) {
4435                 s = get_strip (sur->expand, get_address (msg));
4436         } else {
4437                 s = _select;
4438         }
4439         if (s) {
4440                 if (s->filter_enable_controllable(false)) {
4441                         s->filter_enable_controllable(false)->set_value (s->filter_enable_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4442                         return 0;
4443                 }
4444         }
4445         return sel_fail ("eq_lpf/enable", 0, get_address (msg));
4446 }
4447
4448 int
4449 OSC::sel_eq_hpf_slope (float val, lo_message msg)
4450 {
4451         OSCSurface *sur = get_surface(get_address (msg));
4452         boost::shared_ptr<Stripable> s;
4453         if (sur->expand_enable) {
4454                 s = get_strip (sur->expand, get_address (msg));
4455         } else {
4456                 s = _select;
4457         }
4458         if (s) {
4459                 if (s->filter_slope_controllable(true)) {
4460                         s->filter_slope_controllable(true)->set_value (s->filter_slope_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
4461                         return 0;
4462                 }
4463         }
4464         return sel_fail ("eq_hpf/slope", 0, get_address (msg));
4465 }
4466
4467 int
4468 OSC::sel_eq_lpf_slope (float val, lo_message msg)
4469 {
4470         OSCSurface *sur = get_surface(get_address (msg));
4471         boost::shared_ptr<Stripable> s;
4472         if (sur->expand_enable) {
4473                 s = get_strip (sur->expand, get_address (msg));
4474         } else {
4475                 s = _select;
4476         }
4477         if (s) {
4478                 if (s->filter_slope_controllable(false)) {
4479                         s->filter_slope_controllable(false)->set_value (s->filter_slope_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
4480                         return 0;
4481                 }
4482         }
4483         return sel_fail ("eq_lpf/slope", 0, get_address (msg));
4484 }
4485
4486 int
4487 OSC::sel_eq_gain (int id, float val, lo_message msg)
4488 {
4489         OSCSurface *sur = get_surface(get_address (msg));
4490         boost::shared_ptr<Stripable> s;
4491         if (sur->expand_enable) {
4492                 s = get_strip (sur->expand, get_address (msg));
4493         } else {
4494                 s = _select;
4495         }
4496         if (s) {
4497                 if (id > 0) {
4498                         --id;
4499                 }
4500                 if (s->eq_gain_controllable (id)) {
4501                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4502                         return 0;
4503                 }
4504         }
4505         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
4506 }
4507
4508 int
4509 OSC::sel_eq_freq (int id, float val, lo_message msg)
4510 {
4511         OSCSurface *sur = get_surface(get_address (msg));
4512         boost::shared_ptr<Stripable> s;
4513         if (sur->expand_enable) {
4514                 s = get_strip (sur->expand, get_address (msg));
4515         } else {
4516                 s = _select;
4517         }
4518         if (s) {
4519                 if (id > 0) {
4520                         --id;
4521                 }
4522                 if (s->eq_freq_controllable (id)) {
4523                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4524                         return 0;
4525                 }
4526         }
4527         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
4528 }
4529
4530 int
4531 OSC::sel_eq_q (int id, float val, lo_message msg)
4532 {
4533         OSCSurface *sur = get_surface(get_address (msg));
4534         boost::shared_ptr<Stripable> s;
4535         if (sur->expand_enable) {
4536                 s = get_strip (sur->expand, get_address (msg));
4537         } else {
4538                 s = _select;
4539         }
4540         if (s) {
4541                 if (id > 0) {
4542                         --id;
4543                 }
4544                 if (s->eq_q_controllable (id)) {
4545                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4546                         return 0;
4547                 }
4548         }
4549         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
4550 }
4551
4552 int
4553 OSC::sel_eq_shape (int id, float val, lo_message msg)
4554 {
4555         OSCSurface *sur = get_surface(get_address (msg));
4556         boost::shared_ptr<Stripable> s;
4557         if (sur->expand_enable) {
4558                 s = get_strip (sur->expand, get_address (msg));
4559         } else {
4560                 s = _select;
4561         }
4562         if (s) {
4563                 if (id > 0) {
4564                         --id;
4565                 }
4566                 if (s->eq_shape_controllable (id)) {
4567                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
4568                         return 0;
4569                 }
4570         }
4571         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
4572 }
4573
4574 void
4575 OSC::gui_selection_changed ()
4576 {
4577         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
4578
4579         if (strip) {
4580                 _select = strip;
4581                 for (uint32_t it = 0; it < _surface.size(); ++it) {
4582                         OSCSurface* sur = &_surface[it];
4583                         if(!sur->expand_enable) {
4584                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4585                                 _strip_select (strip, addr);
4586                         }
4587                 }
4588         }
4589 }
4590
4591 // timer callbacks
4592 bool
4593 OSC::periodic (void)
4594 {
4595         if (!tick) {
4596                 Glib::usleep(100); // let flurry of signals subside
4597                 if (global_init) {
4598                         for (uint32_t it = 0; it < _surface.size(); it++) {
4599                                 OSCSurface* sur = &_surface[it];
4600                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
4601                                 global_feedback (sur->feedback, addr, sur->gainmode);
4602                         }
4603                         global_init = false;
4604                         tick = true;
4605                 }
4606                 if (bank_dirty) {
4607                         _recalcbanks ();
4608                         bank_dirty = false;
4609                         tick = true;
4610                 }
4611         }
4612
4613         if (scrub_speed != 0) {
4614                 // for those jog wheels that don't have 0 on release (touch), time out.
4615                 int64_t now = ARDOUR::get_microseconds ();
4616                 int64_t diff = now - scrub_time;
4617                 if (diff > 120000) {
4618                         scrub_speed = 0;
4619                         session->request_transport_speed (0);
4620                         // locate to the place PH was at last tick
4621                         session->request_locate (scrub_place, false);
4622                 }
4623         }
4624
4625         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
4626
4627                 OSCGlobalObserver* go;
4628
4629                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
4630                         go->tick();
4631                 }
4632         }
4633         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
4634
4635                 OSCRouteObserver* ro;
4636
4637                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
4638                         ro->tick();
4639                 }
4640         }
4641         for (uint32_t it = 0; it < _surface.size(); it++) {
4642                 OSCSurface* sur = &_surface[it];
4643                 OSCSelectObserver* so;
4644                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
4645                         so->tick();
4646                 }
4647         }
4648         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
4649
4650                 OSCCueObserver* co;
4651
4652                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4653                         co->tick();
4654                 }
4655         }
4656         for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
4657                 _touch_timeout[(*x).first] = (*x).second - 1;
4658                 if (!(*x).second) {
4659                         boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
4660                         // turn touch off
4661                         ctrl->stop_touch (true, ctrl->session().transport_frame());
4662                         _touch_timeout.erase (x++);
4663                 } else {
4664                         x++;
4665                 }
4666         }
4667         return true;
4668 }
4669
4670 int
4671 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
4672 {
4673         OSCSurface *sur = get_surface(addr);
4674
4675         ostringstream os;
4676         lo_message reply;
4677         if (ssid) {
4678                 reply = lo_message_new ();
4679                 if (sur->feedback[2]) {
4680                         os << "/strip/" << path << "/" << ssid;
4681                 } else {
4682                         os << "/strip/" << path;
4683                         lo_message_add_int32 (reply, ssid);
4684                 }
4685                 string str_pth = os.str();
4686                 lo_message_add_float (reply, (float) val);
4687
4688                 lo_send_message (addr, str_pth.c_str(), reply);
4689                 lo_message_free (reply);
4690         }
4691         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
4692                 os.str("");
4693                 os << "/select/" << path;
4694                 string sel_pth = os.str();
4695                 reply = lo_message_new ();
4696                 lo_message_add_float (reply, (float) val);
4697                 lo_send_message (addr, sel_pth.c_str(), reply);
4698                 lo_message_free (reply);
4699         }
4700
4701         return 0;
4702 }
4703
4704 int
4705 OSC::sel_fail (string path, float val, lo_address addr)
4706 {
4707         ostringstream os;
4708         os.str("");
4709         os << "/select/" << path;
4710         string sel_pth = os.str();
4711         lo_message reply = lo_message_new ();
4712         lo_message_add_float (reply, (float) val);
4713         lo_send_message (addr, sel_pth.c_str(), reply);
4714         lo_message_free (reply);
4715
4716         return 0;
4717 }
4718
4719 int
4720 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
4721 {
4722         OSCSurface *sur = get_surface(addr);
4723
4724         ostringstream os;
4725         lo_message reply;
4726         reply = lo_message_new ();
4727         if (sur->feedback[2]) {
4728                 os << "/select/" << path << "/" << id;
4729         } else {
4730                 os << "/select/" << path;
4731                 lo_message_add_int32 (reply, id);
4732         }
4733         string str_pth = os.str();
4734         lo_message_add_float (reply, (float) val);
4735
4736         lo_send_message (addr, str_pth.c_str(), reply);
4737         lo_message_free (reply);
4738
4739         return 0;
4740 }
4741
4742 XMLNode&
4743 OSC::get_state ()
4744 {
4745         XMLNode& node (ControlProtocol::get_state());
4746         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
4747         node.set_property ("address-only", address_only);
4748         node.set_property ("remote-port", remote_port);
4749         node.set_property ("banksize", default_banksize);
4750         node.set_property ("striptypes", default_strip);
4751         node.set_property ("feedback", default_feedback);
4752         node.set_property ("gainmode", default_gainmode);
4753         node.set_property ("send-page-size", default_send_size);
4754         node.set_property ("plug-page-size", default_plugin_size);
4755         return node;
4756 }
4757
4758 int
4759 OSC::set_state (const XMLNode& node, int version)
4760 {
4761         if (ControlProtocol::set_state (node, version)) {
4762                 return -1;
4763         }
4764         int32_t debugmode;
4765         if (node.get_property (X_("debugmode"), debugmode)) {
4766                 _debugmode = OSCDebugMode (debugmode);
4767         }
4768
4769         node.get_property (X_("address-only"), address_only);
4770         node.get_property (X_("remote-port"), remote_port);
4771         node.get_property (X_("banksize"), default_banksize);
4772         node.get_property (X_("striptypes"), default_strip);
4773         node.get_property (X_("feedback"), default_feedback);
4774         node.get_property (X_("gainmode"), default_gainmode);
4775         node.get_property (X_("send-page-size"), default_send_size);
4776         node.get_property (X_("plugin-page-size"), default_plugin_size);
4777
4778         global_init = true;
4779         tick = false;
4780
4781         return 0;
4782 }
4783
4784 // predicate for sort call in get_sorted_stripables
4785 struct StripableByPresentationOrder
4786 {
4787         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4788         {
4789                 return a->presentation_info().order() < b->presentation_info().order();
4790         }
4791
4792         bool operator () (const Stripable & a, const Stripable & b) const
4793         {
4794                 return a.presentation_info().order() < b.presentation_info().order();
4795         }
4796
4797         bool operator () (const Stripable * a, const Stripable * b) const
4798         {
4799                 return a->presentation_info().order() < b->presentation_info().order();
4800         }
4801 };
4802
4803 OSC::Sorted
4804 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4805 {
4806         Sorted sorted;
4807
4808         // fetch all stripables
4809         StripableList stripables;
4810
4811         session->get_stripables (stripables);
4812
4813         // Look for stripables that match bit in sur->strip_types
4814         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4815
4816                 boost::shared_ptr<Stripable> s = *it;
4817                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4818                         // do nothing... skip it
4819                 } else {
4820
4821                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
4822                                 sorted.push_back (s);
4823                         } else
4824                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
4825                                 sorted.push_back (s);
4826                         } else
4827                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
4828                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4829                                 // r->feeds (session->master_out()) may make more sense
4830                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
4831                                         // this is a bus
4832                                         if (types[2]) {
4833                                                 sorted.push_back (s);
4834                                         }
4835                                 } else {
4836                                         // this is an Aux out
4837                                         if (types[7]) {
4838                                                 sorted.push_back (s);
4839                                         }
4840                                 }
4841                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
4842                                 sorted.push_back (s);
4843                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
4844                                 sorted.push_back (s);
4845                         } else if (types[8] && (s->is_selected())) {
4846                                 sorted.push_back (s);
4847                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4848                                 sorted.push_back (s);
4849                         }
4850 #ifdef MIXBUS
4851                         else if (types[2]) {
4852                                 if (Profile->get_mixbus()) {
4853                                         if (s->mixbus()) {
4854                                                 sorted.push_back (s);
4855                                         }
4856                                 }
4857                         }
4858 #endif
4859                 }
4860         }
4861         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4862         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
4863         if (types[5]) {
4864                 sorted.push_back (session->master_out());
4865         }
4866         if (types[6]) {
4867                 sorted.push_back (session->monitor_out());
4868         }
4869         return sorted;
4870 }
4871
4872 int
4873 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
4874 {
4875         int ret = 1; /* unhandled */
4876
4877         if (!strncmp (path, "/cue/aux", 8)) {
4878                 // set our Aux bus
4879                 ret = cue_set (argv[0]->i, msg);
4880         }
4881         else if (!strncmp (path, "/cue/connect", 12)) {
4882                 // Connect to default Aux bus
4883                 if ((!argc) || argv[0]->i) {
4884                         ret = cue_set (1, msg);
4885                 }
4886         }
4887         else if (!strncmp (path, "/cue/next_aux", 13)) {
4888                 // switch to next Aux bus
4889                 if ((!argc) || argv[0]->i) {
4890                         ret = cue_next (msg);
4891                 }
4892         }
4893         else if (!strncmp (path, "/cue/previous_aux", 17)) {
4894                 // switch to previous Aux bus
4895                 if ((!argc) || argv[0]->i) {
4896                         ret = cue_previous (msg);
4897                 }
4898         }
4899         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
4900                 int id = atoi (&path[16]);
4901                 ret = cue_send_fader (id, argv[0]->f, msg);
4902         }
4903         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
4904                 int id = atoi (&path[17]);
4905                 ret = cue_send_enable (id, argv[0]->f, msg);
4906         }
4907         else if (!strncmp (path, "/cue/fader", 10)) {
4908                 ret = cue_aux_fader (argv[0]->f, msg);
4909         }
4910         else if (!strncmp (path, "/cue/mute", 9)) {
4911                 ret = cue_aux_mute (argv[0]->f, msg);
4912         }
4913
4914         return ret;
4915 }
4916
4917 int
4918 OSC::cue_set (uint32_t aux, lo_message msg)
4919 {
4920         return _cue_set (aux, get_address (msg));
4921 }
4922
4923 int
4924 OSC::_cue_set (uint32_t aux, lo_address addr)
4925 {
4926         int ret = 1;
4927         OSCSurface *s = get_surface(addr);
4928         s->bank_size = 0;
4929         s->strip_types = 128;
4930         s->feedback = 0;
4931         s->gainmode = 1;
4932         s->cue = true;
4933         s->strips = get_sorted_stripables(s->strip_types, s->cue);
4934
4935         s->nstrips = s->strips.size();
4936
4937         if (aux < 1) {
4938                 aux = 1;
4939         } else if (aux > s->nstrips) {
4940                 aux = s->nstrips;
4941         }
4942         s->aux = aux;
4943
4944         // get rid of any old CueObsevers for this address
4945         //cueobserver_connections.drop_connections ();
4946         CueObservers::iterator x;
4947         for (x = cue_observers.begin(); x != cue_observers.end();) {
4948
4949                 OSCCueObserver* co;
4950
4951                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4952
4953                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
4954
4955                         if (res == 0) {
4956                                 delete *x;
4957                                 x = cue_observers.erase (x);
4958                         } else {
4959                                 ++x;
4960                         }
4961                 } else {
4962                         ++x;
4963                 }
4964         }
4965
4966         // get a list of Auxes
4967         for (uint32_t n = 0; n < s->nstrips; ++n) {
4968                 boost::shared_ptr<Stripable> stp = s->strips[n];
4969                 if (stp) {
4970                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
4971                         if (aux == n+1) {
4972                                 // aux must be at least one
4973                                 // need a signal if aux vanishes
4974                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
4975
4976                                 // make a list of stripables with sends that go to this bus
4977                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
4978                                 // start cue observer
4979                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
4980                                 cue_observers.push_back (co);
4981                                 ret = 0;
4982                         }
4983
4984                 }
4985         }
4986
4987         return ret;
4988 }
4989
4990 int
4991 OSC::cue_next (lo_message msg)
4992 {
4993         OSCSurface *s = get_surface(get_address (msg));
4994         int ret = 1;
4995
4996         if (!s->cue) {
4997                 ret = cue_set (1, msg);
4998         }
4999         if (s->aux < s->nstrips) {
5000                 ret = cue_set (s->aux + 1, msg);
5001         } else {
5002                 ret = cue_set (s->nstrips, msg);
5003         }
5004         return ret;
5005 }
5006
5007 int
5008 OSC::cue_previous (lo_message msg)
5009 {
5010         OSCSurface *s = get_surface(get_address (msg));
5011         int ret = 1;
5012         if (!s->cue) {
5013                 ret = cue_set (1, msg);
5014         }
5015         if (s->aux > 1) {
5016                 ret = cue_set (s->aux - 1, msg);
5017         }
5018         return ret;
5019 }
5020
5021 boost::shared_ptr<Send>
5022 OSC::cue_get_send (uint32_t id, lo_address addr)
5023 {
5024         OSCSurface *s = get_surface(addr);
5025         if (id && s->aux > 0 && id <= s->sends.size()) {
5026                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
5027                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
5028                 if (r && aux) {
5029                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5030                 }
5031         }
5032         return boost::shared_ptr<Send>();
5033
5034 }
5035
5036 int
5037 OSC::cue_aux_fader (float position, lo_message msg)
5038 {
5039         if (!session) return -1;
5040
5041         OSCSurface *sur = get_surface(get_address (msg));
5042         if (sur->cue) {
5043                 if (sur->aux) {
5044                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5045
5046                         if (s) {
5047                                 if (s->gain_control()) {
5048                                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
5049                                         return 0;
5050                                 }
5051                         }
5052                 }
5053         }
5054         cue_float_message ("/cue/fader", 0, get_address (msg));
5055         return -1;
5056 }
5057
5058 int
5059 OSC::cue_aux_mute (float state, lo_message msg)
5060 {
5061         if (!session) return -1;
5062
5063         OSCSurface *sur = get_surface(get_address (msg));
5064         if (sur->cue) {
5065                 if (sur->aux) {
5066                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5067                         if (s) {
5068                                 if (s->mute_control()) {
5069                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
5070                                         return 0;
5071                                 }
5072                         }
5073                 }
5074         }
5075         cue_float_message ("/cue/mute", 0, get_address (msg));
5076         return -1;
5077 }
5078
5079 int
5080 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
5081 {
5082         if (!session) {
5083                 return -1;
5084         }
5085         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5086         if (s) {
5087                 if (s->gain_control()) {
5088                         s->gain_control()->set_value (s->gain_control()->interface_to_internal(val), PBD::Controllable::NoGroup);
5089                         return 0;
5090                 }
5091         }
5092         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
5093         return -1;
5094 }
5095
5096 int
5097 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
5098 {
5099         if (!session)
5100                 return -1;
5101         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5102         if (s) {
5103                 if (state) {
5104                         s->activate ();
5105                 } else {
5106                         s->deactivate ();
5107                 }
5108                 return 0;
5109         }
5110         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
5111         return -1;
5112 }
5113
5114 int
5115 OSC::cue_float_message (string path, float val, lo_address addr)
5116 {
5117
5118         lo_message reply;
5119         reply = lo_message_new ();
5120         lo_message_add_float (reply, (float) val);
5121
5122         lo_send_message (addr, path.c_str(), reply);
5123         lo_message_free (reply);
5124
5125         return 0;
5126 }
5127
5128 int
5129 OSC::text_message (string path, string val, lo_address addr)
5130 {
5131
5132         lo_message reply;
5133         reply = lo_message_new ();
5134         lo_message_add_string (reply, val.c_str());
5135
5136         lo_send_message (addr, path.c_str(), reply);
5137         lo_message_free (reply);
5138
5139         return 0;
5140 }
5141
5142
5143 // we have to have a sorted list of stripables that have sends pointed at our aux
5144 // we can use the one in osc.cc to get an aux list
5145 OSC::Sorted
5146 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
5147 {
5148         Sorted sorted;
5149         cueobserver_connections.drop_connections ();
5150         // fetch all stripables
5151         StripableList stripables;
5152
5153         session->get_stripables (stripables);
5154
5155         // Look for stripables that have a send to aux
5156         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5157
5158                 boost::shared_ptr<Stripable> s = *it;
5159                 // we only want routes
5160                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5161                 if (r) {
5162                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
5163                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5164                         if (snd) { // test for send to aux
5165                                 sorted.push_back (s);
5166                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
5167                         }
5168                 }
5169
5170
5171         }
5172         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5173
5174         return sorted;
5175 }
5176