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