OSC: Propagate return value so debug shows errors
[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                 ret = cue_parse (path, types, argv, argc, msg);
877
878         } else
879         if (!strncmp (path, "/access_action/", 15)) {
880                 if (!(argc && !argv[0]->i)) {
881                         std::string action_path = path;
882
883                         access_action (action_path.substr(15));
884                 }
885
886                 ret = 0;
887         } else
888         if (strcmp (path, "/strip/listen") == 0) {
889
890                 cerr << "set up listener\n";
891
892                 lo_message reply = lo_message_new ();
893
894                 if (argc <= 0) {
895                         lo_message_add_string (reply, "syntax error");
896                 } else {
897                         for (int n = 0; n < argc; ++n) {
898
899                                 boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
900
901                                 if (!r) {
902                                         lo_message_add_string (reply, "not found");
903                                         cerr << "no such route\n";
904                                         break;
905                                 } else {
906                                         cerr << "add listener\n";
907                                         listen_to_route (r, get_address (msg));
908                                         lo_message_add_int32 (reply, argv[n]->i);
909                                 }
910                         }
911                 }
912
913                 lo_send_message (get_address (msg), "#reply", reply);
914                 lo_message_free (reply);
915
916                 ret = 0;
917
918         } else
919         if (strcmp (path, "/strip/ignore") == 0) {
920
921                 for (int n = 0; n < argc; ++n) {
922
923                         boost::shared_ptr<Route> r = session->get_remote_nth_route (argv[n]->i);
924
925                         if (r) {
926                                 end_listen (r, get_address (msg));
927                         }
928                 }
929
930                 ret = 0;
931         } else
932         if (!strncmp (path, "/strip/gain/", 12) && strlen (path) > 12) {
933                 // in dB
934                 int ssid = atoi (&path[12]);
935                 ret = route_set_gain_dB (ssid, argv[0]->f, msg);
936         }
937         else if (!strncmp (path, "/strip/fader/", 13) && strlen (path) > 13) {
938                 // in fader position
939                 int ssid = atoi (&path[13]);
940                 ret = route_set_gain_fader (ssid, argv[0]->f, msg);
941         }
942         else if (!strncmp (path, "/strip/trimdB/", 14) && strlen (path) > 14) {
943                 int ssid = atoi (&path[14]);
944                 ret = route_set_trim_dB (ssid, argv[0]->f, msg);
945         }
946         else if (!strncmp (path, "/strip/pan_stereo_position/", 27) && strlen (path) > 27) {
947                 int ssid = atoi (&path[27]);
948                 ret = route_set_pan_stereo_position (ssid, argv[0]->f, msg);
949         }
950         else if (!strncmp (path, "/strip/mute/", 12) && strlen (path) > 12) {
951                 int ssid = atoi (&path[12]);
952                 ret = route_mute (ssid, argv[0]->i, msg);
953         }
954         else if (!strncmp (path, "/strip/solo/", 12) && strlen (path) > 12) {
955                 int ssid = atoi (&path[12]);
956                 ret = route_solo (ssid, argv[0]->i, msg);
957         }
958         else if (!strncmp (path, "/strip/monitor_input/", 21) && strlen (path) > 21) {
959                 int ssid = atoi (&path[21]);
960                 ret = route_monitor_input (ssid, argv[0]->i, msg);
961         }
962         else if (!strncmp (path, "/strip/monitor_disk/", 20) && strlen (path) > 20) {
963                 int ssid = atoi (&path[20]);
964                 ret = route_monitor_disk (ssid, argv[0]->i, msg);
965         }
966         else if (!strncmp (path, "/strip/recenable/", 17) && strlen (path) > 17) {
967                 int ssid = atoi (&path[17]);
968                 ret = route_recenable (ssid, argv[0]->i, msg);
969         }
970         else if (!strncmp (path, "/strip/record_safe/", 19) && strlen (path) > 19) {
971                 int ssid = atoi (&path[19]);
972                 ret = route_recsafe (ssid, argv[0]->i, msg);
973         }
974         else if (!strncmp (path, "/strip/expand/", 14) && strlen (path) > 14) {
975                 int ssid = atoi (&path[14]);
976                 ret = strip_expand (ssid, argv[0]->i, msg);
977         }
978         else if (!strncmp (path, "/strip/select/", 14) && strlen (path) > 14) {
979                 int ssid = atoi (&path[14]);
980                 ret = strip_gui_select (ssid, argv[0]->i, msg);
981         }
982         else if (!strncmp (path, "/select/send_gain/", 18) && strlen (path) > 18) {
983                 int ssid = atoi (&path[18]);
984                 ret = sel_sendgain (ssid, argv[0]->f, msg);
985         }
986         else if (!strncmp (path, "/select/send_fader/", 19) && strlen (path) > 19) {
987                 int ssid = atoi (&path[19]);
988                 ret = sel_sendfader (ssid, argv[0]->f, msg);
989         }
990         else if (!strncmp (path, "/select/send_enable/", 20) && strlen (path) > 20) {
991                 int ssid = atoi (&path[20]);
992                 ret = sel_sendenable (ssid, argv[0]->f, msg);
993         }
994         else if (!strncmp (path, "/select/eq_gain/", 16) && strlen (path) > 16) {
995                 int ssid = atoi (&path[16]);
996                 ret = sel_eq_gain (ssid, argv[0]->f, msg);
997         }
998         else if (!strncmp (path, "/select/eq_freq/", 16) && strlen (path) > 16) {
999                 int ssid = atoi (&path[16]);
1000                 ret = sel_eq_freq (ssid, argv[0]->f , msg);
1001         }
1002         else if (!strncmp (path, "/select/eq_q/", 13) && strlen (path) > 13) {
1003                 int ssid = atoi (&path[13]);
1004                 ret = sel_eq_q (ssid, argv[0]->f, msg);
1005         }
1006         else if (!strncmp (path, "/select/eq_shape/", 17) && strlen (path) > 17) {
1007                 int ssid = atoi (&path[17]);
1008                 ret = sel_eq_shape (ssid, argv[0]->f, msg);
1009         }
1010
1011         if ((ret && _debugmode != Off)) {
1012                 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
1013         } else if (!ret && _debugmode == All) {
1014                 debugmsg (_("OSC"), path, types, argv, argc);
1015         }
1016
1017         return ret;
1018 }
1019
1020 void
1021 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
1022 {
1023         std::stringstream ss;
1024         for (int i = 0; i < argc; ++i) {
1025                 lo_type type = (lo_type)types[i];
1026                         ss << " ";
1027                 switch (type) {
1028                         case LO_INT32:
1029                                 ss << "i:" << argv[i]->i;
1030                                 break;
1031                         case LO_FLOAT:
1032                                 ss << "f:" << argv[i]->f;
1033                                 break;
1034                         case LO_DOUBLE:
1035                                 ss << "d:" << argv[i]->d;
1036                                 break;
1037                         case LO_STRING:
1038                                 ss << "s:" << &argv[i]->s;
1039                                 break;
1040                         case LO_INT64:
1041                                 ss << "h:" << argv[i]->h;
1042                                 break;
1043                         case LO_CHAR:
1044                                 ss << "c:" << argv[i]->s;
1045                                 break;
1046                         case LO_TIMETAG:
1047                                 ss << "<Timetag>";
1048                                 break;
1049                         case LO_BLOB:
1050                                 ss << "<BLOB>";
1051                                 break;
1052                         case LO_TRUE:
1053                                 ss << "#T";
1054                                 break;
1055                         case LO_FALSE:
1056                                 ss << "#F";
1057                                 break;
1058                         case LO_NIL:
1059                                 ss << "NIL";
1060                                 break;
1061                         case LO_INFINITUM:
1062                                 ss << "#inf";
1063                                 break;
1064                         case LO_MIDI:
1065                                 ss << "<MIDI>";
1066                                 break;
1067                         case LO_SYMBOL:
1068                                 ss << "<SYMBOL>";
1069                                 break;
1070                         default:
1071                                 ss << "< ?? >";
1072                                 break;
1073                 }
1074         }
1075         PBD::info << prefix << ": " << path << ss.str() << endmsg;
1076 }
1077
1078 // "Application Hook" Handlers //
1079 void
1080 OSC::session_loaded (Session& s)
1081 {
1082 //      lo_address listener = lo_address_new (NULL, "7770");
1083 //      lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
1084 }
1085
1086 void
1087 OSC::session_exported (std::string path, std::string name)
1088 {
1089         lo_address listener = lo_address_new (NULL, "7770");
1090         lo_send (listener, "/session/exported", "ss", path.c_str(), name.c_str());
1091         lo_address_free (listener);
1092 }
1093
1094 // end "Application Hook" Handlers //
1095
1096 /* path callbacks */
1097
1098 int
1099 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
1100 {
1101 #if 0
1102         const char* returl;
1103
1104         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
1105                 return 1;
1106         }
1107
1108         const char *returl = argv[1]->s;
1109         lo_address addr = find_or_cache_addr (returl);
1110
1111         const char *retpath = argv[2]->s;
1112
1113
1114         if (strcmp (argv[0]->s, "transport_frame") == 0) {
1115
1116                 if (session) {
1117                         lo_send (addr, retpath, "i", session->transport_frame());
1118                 }
1119
1120         } else if (strcmp (argv[0]->s, "transport_speed") == 0) {
1121
1122                 if (session) {
1123                         lo_send (addr, retpath, "i", session->transport_frame());
1124                 }
1125
1126         } else if (strcmp (argv[0]->s, "transport_locked") == 0) {
1127
1128                 if (session) {
1129                         lo_send (addr, retpath, "i", session->transport_frame());
1130                 }
1131
1132         } else if (strcmp (argv[0]->s, "punch_in") == 0) {
1133
1134                 if (session) {
1135                         lo_send (addr, retpath, "i", session->transport_frame());
1136                 }
1137
1138         } else if (strcmp (argv[0]->s, "punch_out") == 0) {
1139
1140                 if (session) {
1141                         lo_send (addr, retpath, "i", session->transport_frame());
1142                 }
1143
1144         } else if (strcmp (argv[0]->s, "rec_enable") == 0) {
1145
1146                 if (session) {
1147                         lo_send (addr, retpath, "i", session->transport_frame());
1148                 }
1149
1150         } else {
1151
1152                 /* error */
1153         }
1154 #endif
1155         return 0;
1156 }
1157
1158 void
1159 OSC::routes_list (lo_message msg)
1160 {
1161         if (!session) {
1162                 return;
1163         }
1164         OSCSurface *sur = get_surface(get_address (msg));
1165         sur->no_clear = true;
1166
1167         for (int n = 0; n < (int) sur->nstrips; ++n) {
1168
1169                 boost::shared_ptr<Stripable> s = get_strip (n + 1, get_address (msg));
1170
1171                 if (s) {
1172                         // some things need the route
1173                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
1174
1175                         lo_message reply = lo_message_new ();
1176
1177                         if (s->presentation_info().flags() & PresentationInfo::AudioTrack) {
1178                                 lo_message_add_string (reply, "AT");
1179                         } else if (s->presentation_info().flags() & PresentationInfo::MidiTrack) {
1180                                 lo_message_add_string (reply, "MT");
1181                         } else if (s->presentation_info().flags() & PresentationInfo::AudioBus) {
1182                                 // r->feeds (session->master_out()) may make more sense
1183                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
1184                                         // this is a bus
1185                                         lo_message_add_string (reply, "B");
1186                                 } else {
1187                                         // this is an Aux out
1188                                         lo_message_add_string (reply, "AX");
1189                                 }
1190                         } else if (s->presentation_info().flags() & PresentationInfo::MidiBus) {
1191                                 lo_message_add_string (reply, "MB");
1192                         } else if (s->presentation_info().flags() & PresentationInfo::VCA) {
1193                                 lo_message_add_string (reply, "V");
1194                         }
1195
1196                         lo_message_add_string (reply, s->name().c_str());
1197                         if (r) {
1198                                 // routes have inputs and outputs
1199                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
1200                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
1201                         } else {
1202                                 // non-routes like VCAs don't
1203                                 lo_message_add_int32 (reply, 0);
1204                                 lo_message_add_int32 (reply, 0);
1205                         }
1206                         if (s->mute_control()) {
1207                                 lo_message_add_int32 (reply, s->mute_control()->get_value());
1208                         } else {
1209                                 lo_message_add_int32 (reply, 0);
1210                         }
1211                         if (s->solo_control()) {
1212                                 lo_message_add_int32 (reply, s->solo_control()->get_value());
1213                         } else {
1214                                 lo_message_add_int32 (reply, 0);
1215                         }
1216                         lo_message_add_int32 (reply, n + 1);
1217                         if (s->rec_enable_control()) {
1218                                 lo_message_add_int32 (reply, s->rec_enable_control()->get_value());
1219                         }
1220
1221                         //Automatically listen to stripables listed
1222                         listen_to_route(s, get_address (msg));
1223
1224                         lo_send_message (get_address (msg), "#reply", reply);
1225                         lo_message_free (reply);
1226                 }
1227         }
1228
1229         // Send end of listing message
1230         lo_message reply = lo_message_new ();
1231
1232         lo_message_add_string (reply, "end_route_list");
1233         lo_message_add_int64 (reply, session->frame_rate());
1234         lo_message_add_int64 (reply, session->current_end_frame());
1235         if (session->monitor_out()) {
1236                 // this session has a monitor section
1237                 lo_message_add_int32 (reply, 1);
1238         } else {
1239                 lo_message_add_int32 (reply, 0);
1240         }
1241
1242         lo_send_message (get_address (msg), "#reply", reply);
1243
1244         lo_message_free (reply);
1245 }
1246
1247 int
1248 OSC::cancel_all_solos ()
1249 {
1250         session->cancel_all_solo ();
1251         return 0;
1252 }
1253
1254 lo_address
1255 OSC::get_address (lo_message msg)
1256 {
1257         if (address_only) {
1258                 lo_address addr = lo_message_get_source (msg);
1259                 string host = lo_address_get_hostname (addr);
1260                 int protocol = lo_address_get_protocol (addr);
1261                 return lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1262         } else {
1263                 return lo_message_get_source (msg);
1264         }
1265 }
1266
1267 int
1268 OSC::refresh_surface (lo_message msg)
1269 {
1270         if (address_only) {
1271                 // get rid of all surfaces and observers.
1272                 // needs change to only clear those for this address on all ports
1273                 clear_devices();
1274         }
1275         OSCSurface *s = get_surface(get_address (msg));
1276         // restart all observers
1277         set_surface (s->bank_size, (uint32_t) s->strip_types.to_ulong(), (uint32_t) s->feedback.to_ulong(), (uint32_t) s->gainmode, msg);
1278         return 0;
1279 }
1280
1281 void
1282 OSC::clear_devices ()
1283 {
1284         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end();) {
1285
1286                 OSCRouteObserver* rc;
1287
1288                 if ((rc = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
1289                         delete *x;
1290                         x = route_observers.erase (x);
1291                 } else {
1292                         ++x;
1293                 }
1294                 // slow devices need time to clear buffers
1295                 usleep ((uint32_t) 10);
1296         }
1297         // Should maybe do global_observers too
1298         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end();) {
1299
1300                 OSCGlobalObserver* gc;
1301
1302                 if ((gc = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1303                         delete *x;
1304                         x = global_observers.erase (x);
1305                 } else {
1306                         ++x;
1307                 }
1308         }
1309         // delete select observers
1310         for (uint32_t it = 0; it < _surface.size(); ++it) {
1311                 OSCSurface* sur = &_surface[it];
1312                 OSCSelectObserver* so;
1313                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
1314                         delete so;
1315                 }
1316         }
1317         // delete cue observers
1318         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
1319                 OSCCueObserver* co;
1320                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
1321                         delete *x;
1322                         x = cue_observers.erase (x);
1323                 } else {
1324                         ++x;
1325                 }
1326         }
1327
1328         // clear out surfaces
1329         _surface.clear();
1330 }
1331
1332 int
1333 OSC::set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gm, lo_message msg)
1334 {
1335         OSCSurface *s = get_surface(get_address (msg));
1336         s->bank_size = b_size;
1337         s->strip_types = strips;
1338         s->feedback = fb;
1339         s->gainmode = gm;
1340         // set bank and strip feedback
1341         set_bank(s->bank, msg);
1342
1343         global_feedback (s->feedback, get_address (msg), s->gainmode);
1344         return 0;
1345 }
1346
1347 int
1348 OSC::set_surface_bank_size (uint32_t bs, lo_message msg)
1349 {
1350         OSCSurface *s = get_surface(get_address (msg));
1351         s->bank_size = bs;
1352
1353         // set bank and strip feedback
1354         set_bank(s->bank, msg);
1355         return 0;
1356 }
1357
1358 int
1359 OSC::set_surface_strip_types (uint32_t st, lo_message msg)
1360 {
1361         OSCSurface *s = get_surface(get_address (msg));
1362         s->strip_types = st;
1363
1364         // set bank and strip feedback
1365         set_bank(s->bank, msg);
1366         return 0;
1367 }
1368
1369
1370 int
1371 OSC::set_surface_feedback (uint32_t fb, lo_message msg)
1372 {
1373         OSCSurface *s = get_surface(get_address (msg));
1374         s->feedback = fb;
1375
1376         // set bank and strip feedback
1377         set_bank(s->bank, msg);
1378
1379         // Set global/master feedback
1380         global_feedback (s->feedback, get_address (msg), s->gainmode);
1381         return 0;
1382 }
1383
1384
1385 int
1386 OSC::set_surface_gainmode (uint32_t gm, lo_message msg)
1387 {
1388         OSCSurface *s = get_surface(get_address (msg));
1389         s->gainmode = gm;
1390
1391         // set bank and strip feedback
1392         set_bank(s->bank, msg);
1393
1394         // Set global/master feedback
1395         global_feedback (s->feedback, get_address (msg), s->gainmode);
1396         return 0;
1397 }
1398
1399 OSC::OSCSurface *
1400 OSC::get_surface (lo_address addr)
1401 {
1402         string r_url;
1403         char * rurl;
1404         rurl = lo_address_get_url (addr);
1405         r_url = rurl;
1406         free (rurl);
1407         for (uint32_t it = 0; it < _surface.size(); ++it) {
1408                 //find setup for this server
1409                 if (!_surface[it].remote_url.find(r_url)){
1410                         return &_surface[it];
1411                 }
1412         }
1413         // if we do this when OSC is started we get the wrong stripable
1414         // we don't need this until we actually have a surface to deal with
1415         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1416                 gui_selection_changed();
1417         }
1418
1419         // No surface create one with default values
1420         OSCSurface s;
1421         s.remote_url = r_url;
1422         s.bank = 1;
1423         s.bank_size = default_banksize; // need to find out how many strips there are
1424         s.strip_types = default_strip; // 159 is tracks, busses, and VCAs (no master/monitor)
1425         s.feedback = default_feedback;
1426         s.gainmode = default_gainmode;
1427         s.sel_obs = 0;
1428         s.expand = 0;
1429         s.expand_enable = false;
1430         s.cue = false;
1431         s.strips = get_sorted_stripables(s.strip_types, s.cue);
1432
1433         s.nstrips = s.strips.size();
1434         _surface.push_back (s);
1435
1436         return &_surface[_surface.size() - 1];
1437 }
1438
1439 // setup global feedback for a surface
1440 void
1441 OSC::global_feedback (bitset<32> feedback, lo_address addr, uint32_t gainmode)
1442 {
1443         // first destroy global observer for this surface
1444         GlobalObservers::iterator x;
1445         for (x = global_observers.begin(); x != global_observers.end();) {
1446
1447                 OSCGlobalObserver* ro;
1448
1449                 if ((ro = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
1450
1451                         int res = strcmp(lo_address_get_url(ro->address()), lo_address_get_url(addr));
1452
1453                         if (res == 0) {
1454                                 delete *x;
1455                                 x = global_observers.erase (x);
1456                         } else {
1457                                 ++x;
1458                         }
1459                 } else {
1460                         ++x;
1461                 }
1462         }
1463         if (feedback[4] || feedback[3] || feedback[5] || feedback[6]) {
1464                 // create a new Global Observer for this surface
1465                 OSCGlobalObserver* o = new OSCGlobalObserver (*session, addr, gainmode, /*s->*/feedback);
1466                 global_observers.push_back (o);
1467         }
1468 }
1469
1470 void
1471 OSC::notify_routes_added (ARDOUR::RouteList &)
1472 {
1473         // not sure if we need this PI change seems to cover
1474         //recalcbanks();
1475 }
1476
1477 void
1478 OSC::notify_vca_added (ARDOUR::VCAList &)
1479 {
1480         // not sure if we need this PI change seems to cover
1481         //recalcbanks();
1482 }
1483
1484 void
1485 OSC::recalcbanks ()
1486 {
1487         tick = false;
1488         bank_dirty = true;
1489 }
1490
1491 void
1492 OSC::_recalcbanks ()
1493 {
1494         if (!_select || (_select != ControlProtocol::first_selected_stripable())) {
1495                 _select = ControlProtocol::first_selected_stripable();
1496         }
1497
1498         // do a set_bank for each surface we know about.
1499         for (uint32_t it = 0; it < _surface.size(); ++it) {
1500                 OSCSurface* sur = &_surface[it];
1501                 // find lo_address
1502                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
1503                 if (sur->cue) {
1504                         _cue_set (sur->aux, addr);
1505                 } else {
1506                         _set_bank (sur->bank, addr);
1507                 }
1508                 if (sur->no_clear) {
1509                         // This surface uses /strip/list tell it routes have changed
1510                         lo_message reply;
1511                         reply = lo_message_new ();
1512                         lo_send_message (addr, "/strip/list", reply);
1513                         lo_message_free (reply);
1514                 }
1515         }
1516 }
1517
1518 /*
1519  * This gets called not only when bank changes but also:
1520  *  - bank size change
1521  *  - feedback change
1522  *  - strip types changes
1523  *  - fadermode changes
1524  *  - stripable creation/deletion/flag
1525  *  - to refresh what is "displayed"
1526  * Basically any time the bank needs to be rebuilt
1527  */
1528 int
1529 OSC::set_bank (uint32_t bank_start, lo_message msg)
1530 {
1531         return _set_bank (bank_start, get_address (msg));
1532 }
1533
1534 // set bank is callable with either message or address
1535 int
1536 OSC::_set_bank (uint32_t bank_start, lo_address addr)
1537 {
1538         if (!session) {
1539                 return -1;
1540         }
1541         // no nstripables yet
1542         if (!session->nroutes()) {
1543                 return -1;
1544         }
1545
1546         OSCSurface *s = get_surface (addr);
1547
1548         // revert any expand to select
1549          s->expand = 0;
1550          s->expand_enable = false;
1551         _strip_select (ControlProtocol::first_selected_stripable(), addr);
1552
1553         // undo all listeners for this url
1554         StripableList stripables;
1555         session->get_stripables (stripables);
1556         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
1557
1558                 boost::shared_ptr<Stripable> stp = *it;
1559                 if (stp) {
1560                         end_listen (stp, addr);
1561                 }
1562                 // slow devices need time to clear buffers
1563                 usleep ((uint32_t) 10);
1564         }
1565
1566         s->strips = get_sorted_stripables(s->strip_types, s->cue);
1567         s->nstrips = s->strips.size();
1568
1569         uint32_t b_size;
1570         if (!s->bank_size) {
1571                 // no banking - bank includes all stripables
1572                 b_size = s->nstrips;
1573         } else {
1574                 b_size = s->bank_size;
1575         }
1576
1577         // Do limits checking
1578         if (bank_start < 1) bank_start = 1;
1579         if (b_size >= s->nstrips)  {
1580                 bank_start = 1;
1581         } else if (bank_start > ((s->nstrips - b_size) + 1)) {
1582                 // top bank is always filled if there are enough strips for at least one bank
1583                 bank_start = (uint32_t)((s->nstrips - b_size) + 1);
1584         }
1585         //save bank after bank limit checks
1586         s->bank = bank_start;
1587
1588         if (s->feedback[0] || s->feedback[1]) {
1589
1590                 for (uint32_t n = bank_start; n < (min ((b_size + bank_start), s->nstrips + 1)); ++n) {
1591                         if (n <= s->strips.size()) {
1592                                 boost::shared_ptr<Stripable> stp = s->strips[n - 1];
1593
1594                                 if (stp) {
1595                                         listen_to_route(stp, addr);
1596                                 }
1597                         }
1598                         // slow devices need time to clear buffers
1599                         usleep ((uint32_t) 10);
1600                 }
1601         }
1602         // light bankup or bankdown buttons if it is possible to bank in that direction
1603         if (s->feedback[4] && !s->no_clear) {
1604                 lo_message reply;
1605                 reply = lo_message_new ();
1606                 if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
1607                         lo_message_add_int32 (reply, 0);
1608                 } else {
1609                         lo_message_add_int32 (reply, 1);
1610                 }
1611                 lo_send_message (addr, "/bank_up", reply);
1612                 lo_message_free (reply);
1613                 reply = lo_message_new ();
1614                 if (s->bank > 1) {
1615                         lo_message_add_int32 (reply, 1);
1616                 } else {
1617                         lo_message_add_int32 (reply, 0);
1618                 }
1619                 lo_send_message (addr, "/bank_down", reply);
1620                 lo_message_free (reply);
1621         }
1622         bank_dirty = false;
1623         tick = true;
1624         return 0;
1625 }
1626
1627 int
1628 OSC::bank_up (lo_message msg)
1629 {
1630         if (!session) {
1631                 return -1;
1632         }
1633         OSCSurface *s = get_surface(get_address (msg));
1634         set_bank (s->bank + s->bank_size, msg);
1635         return 0;
1636 }
1637
1638 int
1639 OSC::bank_down (lo_message msg)
1640 {
1641         if (!session) {
1642                 return -1;
1643         }
1644         OSCSurface *s = get_surface(get_address (msg));
1645         if (s->bank < s->bank_size) {
1646                 set_bank (1, msg);
1647         } else {
1648                 set_bank (s->bank - s->bank_size, msg);
1649         }
1650         return 0;
1651 }
1652
1653 uint32_t
1654 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
1655 {
1656         if (!strip) {
1657                 return 0;
1658         }
1659
1660         OSCSurface *s = get_surface(addr);
1661
1662         uint32_t b_size;
1663         if (!s->bank_size) {
1664                 // no banking
1665                 b_size = s->nstrips;
1666         } else {
1667                 b_size = s->bank_size;
1668         }
1669
1670         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
1671                 if (n <= s->strips.size()) {
1672                         if (strip == s->strips[n-1]) {
1673                                 return n - s->bank + 1;
1674                         }
1675                 }
1676         }
1677         // failsafe... should never get here.
1678         return 0;
1679 }
1680
1681 boost::shared_ptr<ARDOUR::Stripable>
1682 OSC::get_strip (uint32_t ssid, lo_address addr)
1683 {
1684         OSCSurface *s = get_surface(addr);
1685         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
1686                 return s->strips[ssid + s->bank - 2];
1687         }
1688         // guess it is out of range
1689         return boost::shared_ptr<ARDOUR::Stripable>();
1690 }
1691
1692 void
1693 OSC::transport_frame (lo_message msg)
1694 {
1695         if (!session) {
1696                 return;
1697         }
1698         framepos_t pos = session->transport_frame ();
1699
1700         lo_message reply = lo_message_new ();
1701         lo_message_add_int64 (reply, pos);
1702
1703         lo_send_message (get_address (msg), "/transport_frame", reply);
1704
1705         lo_message_free (reply);
1706 }
1707
1708 void
1709 OSC::transport_speed (lo_message msg)
1710 {
1711         if (!session) {
1712                 return;
1713         }
1714         double ts = session->transport_speed ();
1715
1716         lo_message reply = lo_message_new ();
1717         lo_message_add_double (reply, ts);
1718
1719         lo_send_message (get_address (msg), "/transport_speed", reply);
1720
1721         lo_message_free (reply);
1722 }
1723
1724 void
1725 OSC::record_enabled (lo_message msg)
1726 {
1727         if (!session) {
1728                 return;
1729         }
1730         int re = (int)session->get_record_enabled ();
1731
1732         lo_message reply = lo_message_new ();
1733         lo_message_add_int32 (reply, re);
1734
1735         lo_send_message (get_address (msg), "/record_enabled", reply);
1736
1737         lo_message_free (reply);
1738 }
1739
1740 int
1741 OSC::scrub (float delta, lo_message msg)
1742 {
1743         if (!session) return -1;
1744
1745         scrub_place = session->transport_frame ();
1746
1747         float speed;
1748
1749         int64_t now = ARDOUR::get_microseconds ();
1750         int64_t diff = now - scrub_time;
1751         if (diff > 35000) {
1752                 // speed 1 (or 0 if jog wheel supports touch)
1753                 speed = delta;
1754         } else if ((diff > 20000) && (fabs(scrub_speed) == 1)) {
1755                 // add some hysteresis to stop excess speed jumps
1756                 speed = delta;
1757         } else {
1758                 speed = (int)(delta * 2);
1759         }
1760         scrub_time = now;
1761         if (scrub_speed == speed) {
1762                 // Already at that speed no change
1763                 return 0;
1764         }
1765         scrub_speed = speed;
1766
1767         if (speed > 0) {
1768                 if (speed == 1) {
1769                         session->request_transport_speed (.5);
1770                 } else {
1771                         session->request_transport_speed (1);
1772                 }
1773         } else if (speed < 0) {
1774                 if (speed == -1) {
1775                         session->request_transport_speed (-.5);
1776                 } else {
1777                         session->request_transport_speed (-1);
1778                 }
1779         } else {
1780                 session->request_transport_speed (0);
1781         }
1782
1783         return 0;
1784 }
1785
1786 int
1787 OSC::jog (float delta, lo_message msg)
1788 {
1789         if (!session) return -1;
1790
1791         OSCSurface *s = get_surface(get_address (msg));
1792
1793         string path = "/jog/mode/name";
1794         switch(s->jogmode)
1795         {
1796                 case JOG  :
1797                         text_message (path, "Jog", get_address (msg));
1798                         if (delta) {
1799                                 jump_by_seconds (delta / 5);
1800                         }
1801                         break;
1802                 case SCRUB:
1803                         text_message (path, "Scrub", get_address (msg));
1804                         scrub (delta, msg);
1805                         break;
1806                 case SHUTTLE:
1807                         text_message (path, "Shuttle", get_address (msg));
1808                         if (delta) {
1809                                 double speed = get_transport_speed ();
1810                                 set_transport_speed (speed + (delta / 8));
1811                         } else {
1812                                 set_transport_speed (0);
1813                         }
1814                         break;
1815                 case SCROLL:
1816                         text_message (path, "Scroll", get_address (msg));
1817                         if (delta > 0) {
1818                                 access_action ("Editor/scroll-forward");
1819                         } else if (delta < 0) {
1820                                 access_action ("Editor/scroll-backward");
1821                         }
1822                         break;
1823                 case TRACK:
1824                         text_message (path, "Track", get_address (msg));
1825                         if (delta > 0) {
1826                                 set_bank (s->bank + 1, msg);
1827                         } else if (delta < 0) {
1828                                 set_bank (s->bank - 1, msg);
1829                         }
1830                         break;
1831                 case BANK:
1832                         text_message (path, "Bank", get_address (msg));
1833                         if (delta > 0) {
1834                                 bank_up (msg);
1835                         } else if (delta < 0) {
1836                                 bank_down (msg);
1837                         }
1838                         break;
1839                 case NUDGE:
1840                         text_message (path, "Nudge", get_address (msg));
1841                         if (delta > 0) {
1842                                 access_action ("Common/nudge-playhead-forward");
1843                         } else if (delta < 0) {
1844                                 access_action ("Common/nudge-playhead-backward");
1845                         }
1846                         break;
1847                 case MARKER:
1848                         text_message (path, "Marker", get_address (msg));
1849                         if (delta > 0) {
1850                                 next_marker ();
1851                         } else if (delta < 0) {
1852                                 prev_marker ();
1853                         }
1854                         break;
1855                 default:
1856                         break;
1857
1858         }
1859         return 0;
1860
1861 }
1862
1863 int
1864 OSC::jog_mode (float mode, lo_message msg)
1865 {
1866         if (!session) return -1;
1867
1868         OSCSurface *s = get_surface(get_address (msg));
1869
1870         switch((uint32_t)mode)
1871         {
1872                 case JOG  :
1873                         s->jogmode = JOG;
1874                         break;
1875                 case SCRUB:
1876                         s->jogmode = SCRUB;
1877                         break;
1878                 case SHUTTLE:
1879                         s->jogmode = SHUTTLE;
1880                         break;
1881                 case SCROLL:
1882                         s->jogmode = SCROLL;
1883                         break;
1884                 case TRACK:
1885                         s->jogmode = TRACK;
1886                         break;
1887                 case BANK:
1888                         s->jogmode = BANK;
1889                         break;
1890                 case NUDGE:
1891                         s->jogmode = NUDGE;
1892                         break;
1893                 case MARKER:
1894                         s->jogmode = MARKER;
1895                         break;
1896                 default:
1897                         PBD::warning << "Jog Mode: " << mode << " is not valid." << endmsg;
1898                         break;
1899         lo_message reply = lo_message_new ();
1900         lo_message_add_int32 (reply, s->jogmode);
1901         lo_send_message (get_address(msg), "/jog/mode", reply);
1902         lo_message_free (reply);
1903
1904         }
1905         jog (0, msg);
1906         return 0;
1907
1908 }
1909
1910 // master and monitor calls
1911 int
1912 OSC::master_set_gain (float dB)
1913 {
1914         if (!session) return -1;
1915         boost::shared_ptr<Stripable> s = session->master_out();
1916         if (s) {
1917                 if (dB < -192) {
1918                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
1919                 } else {
1920                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1921                 }
1922         }
1923         return 0;
1924 }
1925
1926 int
1927 OSC::master_set_fader (float position)
1928 {
1929         if (!session) return -1;
1930         boost::shared_ptr<Stripable> s = session->master_out();
1931         if (s) {
1932                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
1933         }
1934         return 0;
1935 }
1936
1937 int
1938 OSC::master_set_trim (float dB)
1939 {
1940         if (!session) return -1;
1941         boost::shared_ptr<Stripable> s = session->master_out();
1942
1943         if (s) {
1944                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
1945         }
1946
1947         return 0;
1948 }
1949
1950 int
1951 OSC::master_set_pan_stereo_position (float position, lo_message msg)
1952 {
1953         if (!session) return -1;
1954
1955         float endposition = .5;
1956         boost::shared_ptr<Stripable> s = session->master_out();
1957
1958         if (s) {
1959                 if (s->pan_azimuth_control()) {
1960                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
1961                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
1962                 }
1963         }
1964         OSCSurface *sur = get_surface(get_address (msg));
1965
1966         if (sur->feedback[4]) {
1967                 lo_message reply = lo_message_new ();
1968                 lo_message_add_float (reply, endposition);
1969
1970                 lo_send_message (get_address (msg), "/master/pan_stereo_position", reply);
1971                 lo_message_free (reply);
1972         }
1973
1974         return 0;
1975 }
1976
1977 int
1978 OSC::master_set_mute (uint32_t state)
1979 {
1980         if (!session) return -1;
1981
1982         boost::shared_ptr<Stripable> s = session->master_out();
1983
1984         if (s) {
1985                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
1986         }
1987
1988         return 0;
1989 }
1990
1991 int
1992 OSC::monitor_set_gain (float dB)
1993 {
1994         if (!session) return -1;
1995         boost::shared_ptr<Stripable> s = session->monitor_out();
1996
1997         if (s) {
1998                 if (dB < -192) {
1999                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
2000                 } else {
2001                         s->gain_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
2002                 }
2003         }
2004         return 0;
2005 }
2006
2007 int
2008 OSC::monitor_set_fader (float position)
2009 {
2010         if (!session) return -1;
2011         boost::shared_ptr<Stripable> s = session->monitor_out();
2012         if (s) {
2013                 s->gain_control()->set_value (slider_position_to_gain_with_max (position, 2.0), PBD::Controllable::NoGroup);
2014         }
2015         return 0;
2016 }
2017
2018 int
2019 OSC::monitor_set_mute (uint32_t state)
2020 {
2021         if (!session) return -1;
2022
2023         if (session->monitor_out()) {
2024                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2025                 mon->set_cut_all (state);
2026         }
2027         return 0;
2028 }
2029
2030 int
2031 OSC::monitor_set_dim (uint32_t state)
2032 {
2033         if (!session) return -1;
2034
2035         if (session->monitor_out()) {
2036                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2037                 mon->set_dim_all (state);
2038         }
2039         return 0;
2040 }
2041
2042 int
2043 OSC::monitor_set_mono (uint32_t state)
2044 {
2045         if (!session) return -1;
2046
2047         if (session->monitor_out()) {
2048                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
2049                 mon->set_mono (state);
2050         }
2051         return 0;
2052 }
2053
2054 int
2055 OSC::route_get_sends(lo_message msg) {
2056         if (!session) {
2057                 return -1;
2058         }
2059
2060         lo_arg **argv = lo_message_get_argv(msg);
2061
2062         int rid = argv[0]->i;
2063
2064         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2065         if (!strip) {
2066                 return -1;
2067         }
2068
2069         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2070         if (!r) {
2071                 return -1;
2072         }
2073
2074         lo_message reply = lo_message_new();
2075         lo_message_add_int32(reply, rid);
2076
2077         int i = 0;
2078         for (;;) {
2079                 boost::shared_ptr<Processor> p = r->nth_send(i++);
2080
2081                 if (!p) {
2082                         break;
2083                 }
2084
2085                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2086                 if (isend) {
2087                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
2088                         lo_message_add_string(reply, isend->name().c_str());
2089                         lo_message_add_int32(reply, i);
2090                         boost::shared_ptr<Amp> a = isend->amp();
2091                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
2092                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2093                 }
2094         }
2095         // if used dedicated message path to identify this reply in async operation.
2096         // Naming it #reply wont help the client to identify the content.
2097         lo_send_message(get_address (msg), "/strip/sends", reply);
2098
2099         lo_message_free(reply);
2100
2101         return 0;
2102 }
2103
2104 int
2105 OSC::route_get_receives(lo_message msg) {
2106         if (!session) {
2107                 return -1;
2108         }
2109
2110         lo_arg **argv = lo_message_get_argv(msg);
2111
2112         uint32_t rid = argv[0]->i;
2113
2114
2115         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
2116         if (!strip) {
2117                 return -1;
2118         }
2119
2120         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
2121         if (!r) {
2122                 return -1;
2123         }
2124
2125         boost::shared_ptr<RouteList> route_list = session->get_routes();
2126
2127         lo_message reply = lo_message_new();
2128
2129         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
2130                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
2131                 if (!tr) {
2132                         continue;
2133                 }
2134                 int j = 0;
2135
2136                 for (;;) {
2137                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
2138
2139                         if (!p) {
2140                                 break;
2141                         }
2142
2143                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
2144                         if (isend) {
2145                                 if( isend->target_route()->id() == r->id()){
2146                                         boost::shared_ptr<Amp> a = isend->amp();
2147
2148                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
2149                                         lo_message_add_string(reply, tr->name().c_str());
2150                                         lo_message_add_int32(reply, j);
2151                                         lo_message_add_float(reply, gain_to_slider_position(a->gain_control()->get_value()));
2152                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
2153                                 }
2154                         }
2155                 }
2156         }
2157
2158         // I have used a dedicated message path to identify this reply in async operation.
2159         // Naming it #reply wont help the client to identify the content.
2160         lo_send_message(get_address (msg), "/strip/receives", reply);
2161         lo_message_free(reply);
2162         return 0;
2163 }
2164
2165 // strip calls
2166
2167 int
2168 OSC::set_automation (const char *path, size_t len, lo_arg **argv, int argc, lo_message msg)
2169 {
2170         if (!session) return -1;
2171
2172         int ret = 1;
2173         OSCSurface *sur = get_surface(get_address (msg));
2174         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
2175         uint32_t ctr = 0;
2176         uint32_t aut = 0;
2177
2178         //parse path first to find stripable
2179         if (!strncmp (path, "/strip/", 7)) {
2180                 // find ssid and stripable
2181                 if (argc > 1) {
2182                         strp = get_strip (argv[0]->i, get_address (msg));
2183                         if (argv[1]->f) {
2184                                 aut = (int)argv[1]->f;
2185                         } else {
2186                                 aut = argv[1]->i;
2187                         }
2188                 } else {
2189                         uint32_t ssid = atoi (&(strrchr (path, '/' ))[1]);
2190                         strp = get_strip (ssid, get_address (msg));
2191                         if (argv[0]->f) {
2192                                 aut = (int)argv[0]->f;
2193                         } else {
2194                                 aut = argv[0]->i;
2195                         }
2196                 }
2197                 ctr = 7;
2198         } else if (!strncmp (path, "/select/", 8)) {
2199                 if (sur->expand_enable && sur->expand) {
2200                         strp = get_strip (sur->expand, get_address (msg));
2201                 } else {
2202                         strp = ControlProtocol::first_selected_stripable();
2203                 }
2204                 if (argv[0]->f) {
2205                         aut = (int)argv[0]->f;
2206                 } else {
2207                         aut = argv[0]->i;
2208                 }
2209                 ctr = 8;
2210         } else {
2211                 return ret;
2212         }
2213         if (strp) {
2214                 if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
2215                         if (strp->gain_control ()) {
2216                                 switch (aut) {
2217                                         case 0:
2218                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Off);
2219                                                 ret = 0;
2220                                                 break;
2221                                         case 'm':
2222                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Off);
2223                                                 ret = 0;
2224                                                 break;
2225                                         case 1:
2226                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Play);
2227                                                 ret = 0;
2228                                                 break;
2229                                         case 'p':
2230                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Play);
2231                                                 ret = 0;
2232                                                 break;
2233                                         case 2:
2234                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Write);
2235                                                 ret = 0;
2236                                                 break;
2237                                         case 'w':
2238                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Write);
2239                                                 ret = 0;
2240                                                 break;
2241                                         case 3:
2242                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Touch);
2243                                                 ret = 0;
2244                                                 break;
2245                                         case 't':
2246                                                 strp->gain_control()->set_automation_state (ARDOUR::AutoState::Touch);
2247                                                 ret = 0;
2248                                                 break;
2249                                         default:
2250                                                 break;
2251                                 }
2252                         }
2253                 }
2254         }
2255
2256         return ret;
2257 }
2258
2259 int
2260 OSC::route_mute (int ssid, int yn, lo_message msg)
2261 {
2262         if (!session) return -1;
2263         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2264
2265         if (s) {
2266                 if (s->mute_control()) {
2267                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2268                         return 0;
2269                 }
2270         }
2271
2272         return route_send_fail ("mute", ssid, 0, get_address (msg));
2273 }
2274
2275 int
2276 OSC::sel_mute (uint32_t yn, lo_message msg)
2277 {
2278         OSCSurface *sur = get_surface(get_address (msg));
2279         boost::shared_ptr<Stripable> s;
2280         if (sur->expand_enable) {
2281                 s = get_strip (sur->expand, get_address (msg));
2282         } else {
2283                 s = _select;
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         return sel_fail ("mute", 0, get_address (msg));
2292 }
2293
2294 int
2295 OSC::route_solo (int ssid, int yn, lo_message msg)
2296 {
2297         if (!session) return -1;
2298         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2299
2300         if (s) {
2301                 if (s->solo_control()) {
2302                         s->solo_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2303                 }
2304         }
2305
2306         return route_send_fail ("solo", ssid, 0, get_address (msg));
2307 }
2308
2309 int
2310 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
2311 {
2312         if (!session) return -1;
2313         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2314
2315         if (s) {
2316                 if (s->solo_isolate_control()) {
2317                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2318                         return 0;
2319                 }
2320         }
2321
2322         return route_send_fail ("solo_iso", ssid, 0, get_address (msg));
2323 }
2324
2325 int
2326 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
2327 {
2328         if (!session) return -1;
2329         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
2330
2331         if (s) {
2332                 if (s->solo_safe_control()) {
2333                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2334                         return 0;
2335                 }
2336         }
2337
2338         return route_send_fail ("solo_safe", ssid, 0, get_address (msg));
2339 }
2340
2341 int
2342 OSC::sel_solo (uint32_t yn, lo_message msg)
2343 {
2344         OSCSurface *sur = get_surface(get_address (msg));
2345         boost::shared_ptr<Stripable> s;
2346         if (sur->expand_enable) {
2347                 s = get_strip (sur->expand, get_address (msg));
2348         } else {
2349                 s = _select;
2350         }
2351         if (s) {
2352                 if (s->solo_control()) {
2353                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2354                 }
2355         }
2356         return sel_fail ("solo", 0, get_address (msg));
2357 }
2358
2359 int
2360 OSC::sel_solo_iso (uint32_t yn, lo_message msg)
2361 {
2362         OSCSurface *sur = get_surface(get_address (msg));
2363         boost::shared_ptr<Stripable> s;
2364         if (sur->expand_enable) {
2365                 s = get_strip (sur->expand, get_address (msg));
2366         } else {
2367                 s = _select;
2368         }
2369         if (s) {
2370                 if (s->solo_isolate_control()) {
2371                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2372                         return 0;
2373                 }
2374         }
2375         return sel_fail ("solo_iso", 0, get_address (msg));
2376 }
2377
2378 int
2379 OSC::sel_solo_safe (uint32_t yn, lo_message msg)
2380 {
2381         OSCSurface *sur = get_surface(get_address (msg));
2382         boost::shared_ptr<Stripable> s;
2383         if (sur->expand_enable) {
2384                 s = get_strip (sur->expand, get_address (msg));
2385         } else {
2386                 s = _select;
2387         }
2388         if (s) {
2389                 if (s->solo_safe_control()) {
2390                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2391                         return 0;
2392                 }
2393         }
2394         return sel_fail ("solo_safe", 0, get_address (msg));
2395 }
2396
2397 int
2398 OSC::sel_recenable (uint32_t yn, lo_message msg)
2399 {
2400         OSCSurface *sur = get_surface(get_address (msg));
2401         boost::shared_ptr<Stripable> s;
2402         if (sur->expand_enable) {
2403                 s = get_strip (sur->expand, get_address (msg));
2404         } else {
2405                 s = _select;
2406         }
2407         if (s) {
2408                 if (s->rec_enable_control()) {
2409                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2410                         if (s->rec_enable_control()->get_value()) {
2411                                 return 0;
2412                         }
2413                 }
2414         }
2415         return sel_fail ("recenable", 0, get_address (msg));
2416 }
2417
2418 int
2419 OSC::route_recenable (int ssid, int yn, lo_message msg)
2420 {
2421         if (!session) return -1;
2422         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2423
2424         if (s) {
2425                 if (s->rec_enable_control()) {
2426                         s->rec_enable_control()->set_value (yn, PBD::Controllable::UseGroup);
2427                         if (s->rec_enable_control()->get_value()) {
2428                                 return 0;
2429                         }
2430                 }
2431         }
2432         return route_send_fail ("recenable", ssid, 0, get_address (msg));
2433 }
2434
2435 int
2436 OSC::route_rename(int ssid, char *newname, lo_message msg) {
2437     if (!session) {
2438         return -1;
2439     }
2440
2441     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
2442
2443     if (s) {
2444         s->set_name(std::string(newname));
2445     }
2446
2447     return 0;
2448 }
2449
2450 int
2451 OSC::sel_recsafe (uint32_t yn, lo_message msg)
2452 {
2453         OSCSurface *sur = get_surface(get_address (msg));
2454         boost::shared_ptr<Stripable> s;
2455         if (sur->expand_enable) {
2456                 s = get_strip (sur->expand, get_address (msg));
2457         } else {
2458                 s = _select;
2459         }
2460         if (s) {
2461                 if (s->rec_safe_control()) {
2462                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2463                         if (s->rec_safe_control()->get_value()) {
2464                                 return 0;
2465                         }
2466                 }
2467         }
2468         return sel_fail ("record_safe", 0, get_address (msg));
2469 }
2470
2471 int
2472 OSC::route_recsafe (int ssid, int yn, lo_message msg)
2473 {
2474         if (!session) return -1;
2475         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2476         if (s) {
2477                 if (s->rec_safe_control()) {
2478                         s->rec_safe_control()->set_value (yn, PBD::Controllable::UseGroup);
2479                         if (s->rec_safe_control()->get_value()) {
2480                                 return 0;
2481                         }
2482                 }
2483         }
2484         return route_send_fail ("record_safe", ssid, 0,get_address (msg));
2485 }
2486
2487 int
2488 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
2489 {
2490         if (!session) return -1;
2491         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2492
2493         if (s) {
2494                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2495                 if (track) {
2496                         if (track->monitoring_control()) {
2497                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2498                                 return 0;
2499                         }
2500                 }
2501         }
2502
2503         return route_send_fail ("monitor_input", ssid, 0, get_address (msg));
2504 }
2505
2506 int
2507 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
2508 {
2509         OSCSurface *sur = get_surface(get_address (msg));
2510         boost::shared_ptr<Stripable> s;
2511         if (sur->expand_enable) {
2512                 s = get_strip (sur->expand, get_address (msg));
2513         } else {
2514                 s = _select;
2515         }
2516         if (s) {
2517                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2518                 if (track) {
2519                         if (track->monitoring_control()) {
2520                                 track->monitoring_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2521                                 return 0;
2522                         }
2523                 }
2524         }
2525         return sel_fail ("monitor_input", 0, get_address (msg));
2526 }
2527
2528 int
2529 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
2530 {
2531         if (!session) return -1;
2532         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2533
2534         if (s) {
2535                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2536                 if (track) {
2537                         if (track->monitoring_control()) {
2538                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2539                                 return 0;
2540                         }
2541                 }
2542         }
2543
2544         return route_send_fail ("monitor_disk", ssid, 0, get_address (msg));
2545 }
2546
2547 int
2548 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
2549 {
2550         OSCSurface *sur = get_surface(get_address (msg));
2551         boost::shared_ptr<Stripable> s;
2552         if (sur->expand_enable) {
2553                 s = get_strip (sur->expand, get_address (msg));
2554         } else {
2555                 s = _select;
2556         }
2557         if (s) {
2558                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
2559                 if (track) {
2560                         if (track->monitoring_control()) {
2561                                 track->monitoring_control()->set_value (yn ? 2.0 : 0.0, PBD::Controllable::NoGroup);
2562                                 return 0;
2563                         }
2564                 }
2565         }
2566         return sel_fail ("monitor_disk", 0, get_address (msg));
2567 }
2568
2569
2570 int
2571 OSC::strip_phase (int ssid, int yn, lo_message msg)
2572 {
2573         if (!session) return -1;
2574         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2575
2576         if (s) {
2577                 if (s->phase_control()) {
2578                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
2579                         return 0;
2580                 }
2581         }
2582
2583         return route_send_fail ("polarity", ssid, 0, get_address (msg));
2584 }
2585
2586 int
2587 OSC::sel_phase (uint32_t yn, lo_message msg)
2588 {
2589         OSCSurface *sur = get_surface(get_address (msg));
2590         boost::shared_ptr<Stripable> s;
2591         if (sur->expand_enable) {
2592                 s = get_strip (sur->expand, get_address (msg));
2593         } else {
2594                 s = _select;
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         return sel_fail ("polarity", 0, get_address (msg));
2603 }
2604
2605 int
2606 OSC::strip_expand (int ssid, int yn, lo_message msg)
2607 {
2608         OSCSurface *sur = get_surface(get_address (msg));
2609         sur->expand_enable = (bool) yn;
2610         sur->expand = ssid;
2611         boost::shared_ptr<Stripable> s;
2612         if (yn) {
2613                 s = get_strip (ssid, get_address (msg));
2614         } else {
2615                 s = ControlProtocol::first_selected_stripable();
2616         }
2617
2618         return _strip_select (s, get_address (msg));
2619 }
2620
2621 int
2622 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
2623 {
2624         if (!session) {
2625                 return -1;
2626         }
2627         OSCSurface *sur = get_surface(addr);
2628         if (sur->sel_obs) {
2629                 delete sur->sel_obs;
2630                 sur->sel_obs = 0;
2631         }
2632         bool feedback_on = sur->feedback.to_ulong();
2633         if (s && feedback_on) {
2634                 OSCSelectObserver* sel_fb = new OSCSelectObserver (s, addr, sur->gainmode, sur->feedback);
2635                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2636                 sur->sel_obs = sel_fb;
2637         } else if (sur->expand_enable) {
2638                 sur->expand = 0;
2639                 sur->expand_enable = false;
2640                 if (_select && feedback_on) {
2641                         OSCSelectObserver* sel_fb = new OSCSelectObserver (_select, addr, sur->gainmode, sur->feedback);
2642                         _select->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
2643                         sur->sel_obs = sel_fb;
2644                 }
2645         } else if (feedback_on) {
2646                 route_send_fail ("select", sur->expand, 0 , addr);
2647         }
2648         if (!feedback_on) {
2649                 return 0;
2650         }
2651         //update buttons on surface
2652         int b_s = sur->bank_size;
2653         if (!b_s) { // bank size 0 means we need to know how many strips there are.
2654                 b_s = sur->nstrips;
2655         }
2656         for (int i = 1;  i <= b_s; i++) {
2657                 string path = "expand";
2658
2659                 if ((i == (int) sur->expand) && sur->expand_enable) {
2660                         lo_message reply = lo_message_new ();
2661                         if (sur->feedback[2]) {
2662                                 ostringstream os;
2663                                 os << "/strip/" << path << "/" << i;
2664                                 path = os.str();
2665                         } else {
2666                                 ostringstream os;
2667                                 os << "/strip/" << path;
2668                                 path = os.str();
2669                                 lo_message_add_int32 (reply, i);
2670                         }
2671                         lo_message_add_float (reply, (float) 1);
2672
2673                         lo_send_message (addr, path.c_str(), reply);
2674                         lo_message_free (reply);
2675                         reply = lo_message_new ();
2676                         lo_message_add_float (reply, 1.0);
2677                         lo_send_message (addr, "/select/expand", reply);
2678                         lo_message_free (reply);
2679
2680                 } else {
2681                         lo_message reply = lo_message_new ();
2682                         lo_message_add_int32 (reply, i);
2683                         lo_message_add_float (reply, 0.0);
2684                         lo_send_message (addr, "/strip/expand", reply);
2685                         lo_message_free (reply);
2686                 }
2687         }
2688         if (!sur->expand_enable) {
2689                 lo_message reply = lo_message_new ();
2690                 lo_message_add_float (reply, 0.0);
2691                 lo_send_message (addr, "/select/expand", reply);
2692                 lo_message_free (reply);
2693         }
2694
2695         return 0;
2696 }
2697
2698 int
2699 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
2700 {
2701         //ignore button release
2702         if (!yn) return 0;
2703
2704         if (!session) {
2705                 return -1;
2706         }
2707         OSCSurface *sur = get_surface(get_address (msg));
2708         sur->expand_enable = false;
2709         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2710         if (s) {
2711                 SetStripableSelection (s);
2712         } else {
2713                 if ((int) (sur->feedback.to_ulong())) {
2714                         route_send_fail ("select", ssid, 0, get_address (msg));
2715                 }
2716         }
2717
2718         return 0;
2719 }
2720
2721 int
2722 OSC::sel_expand (uint32_t state, lo_message msg)
2723 {
2724         OSCSurface *sur = get_surface(get_address (msg));
2725         boost::shared_ptr<Stripable> s;
2726         sur->expand_enable = (bool) state;
2727         if (state && sur->expand) {
2728                 s = get_strip (sur->expand, get_address (msg));
2729         } else {
2730                 s = ControlProtocol::first_selected_stripable();
2731         }
2732
2733         return _strip_select (s, get_address (msg));
2734 }
2735
2736 int
2737 OSC::route_set_gain_abs (int ssid, float level, lo_message msg)
2738 {
2739         if (!session) return -1;
2740         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2741
2742         if (s) {
2743                 if (s->gain_control()) {
2744                         s->gain_control()->set_value (level, PBD::Controllable::NoGroup);
2745                 } else {
2746                         return 1;
2747                 }
2748         } else {
2749                 return 1;
2750         }
2751
2752         return 0;
2753 }
2754
2755 int
2756 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
2757 {
2758         if (!session) {
2759                 route_send_fail ("gain", ssid, -193, get_address (msg));
2760                 return -1;
2761         }
2762         int ret;
2763         if (dB < -192) {
2764                 ret = route_set_gain_abs (ssid, 0.0, msg);
2765         } else {
2766                 ret = route_set_gain_abs (ssid, dB_to_coefficient (dB), msg);
2767         }
2768         if (ret != 0) {
2769                 return route_send_fail ("gain", ssid, -193, get_address (msg));
2770         }
2771         return 0;
2772 }
2773
2774 int
2775 OSC::sel_gain (float val, lo_message msg)
2776 {
2777         OSCSurface *sur = get_surface(get_address (msg));
2778         boost::shared_ptr<Stripable> s;
2779         if (sur->expand_enable) {
2780                 s = get_strip (sur->expand, get_address (msg));
2781         } else {
2782                 s = _select;
2783         }
2784         if (s) {
2785                 float abs;
2786                 if (val < -192) {
2787                         abs = 0;
2788                 } else {
2789                         abs = dB_to_coefficient (val);
2790                 }
2791                 if (s->gain_control()) {
2792                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2793                         return 0;
2794                 }
2795         }
2796         return sel_fail ("gain", -193, get_address (msg));
2797 }
2798
2799 int
2800 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
2801 {
2802         if (!session) {
2803                 route_send_fail ("fader", ssid, 0, get_address (msg));
2804                 return -1;
2805         }
2806         int ret;
2807         ret = route_set_gain_abs (ssid, slider_position_to_gain_with_max (pos, 2.0), msg);
2808         if (ret != 0) {
2809                 return route_send_fail ("fader", ssid, 0, get_address (msg));
2810         }
2811         return 0;
2812 }
2813
2814 int
2815 OSC::sel_fader (float val, lo_message msg)
2816 {
2817         OSCSurface *sur = get_surface(get_address (msg));
2818         boost::shared_ptr<Stripable> s;
2819         if (sur->expand_enable) {
2820                 s = get_strip (sur->expand, get_address (msg));
2821         } else {
2822                 s = _select;
2823         }
2824         if (s) {
2825                 float abs;
2826                 abs = slider_position_to_gain_with_max (val, 2.0);
2827                 if (s->gain_control()) {
2828                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
2829                         return 0;
2830                 }
2831         }
2832         return sel_fail ("fader", 0, get_address (msg));
2833 }
2834
2835 int
2836 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
2837 {
2838         if (!session) return -1;
2839         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2840
2841         if (s) {
2842                 if (s->trim_control()) {
2843                         s->trim_control()->set_value (level, PBD::Controllable::NoGroup);
2844                         return 0;
2845                 }
2846
2847         }
2848
2849         return -1;
2850 }
2851
2852 int
2853 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
2854 {
2855         int ret;
2856         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
2857         if (ret != 0) {
2858                 return route_send_fail ("trimdB", ssid, 0, get_address (msg));
2859         }
2860
2861 return 0;
2862 }
2863
2864 int
2865 OSC::sel_trim (float val, lo_message msg)
2866 {
2867         OSCSurface *sur = get_surface(get_address (msg));
2868         boost::shared_ptr<Stripable> s;
2869         if (sur->expand_enable) {
2870                 s = get_strip (sur->expand, get_address (msg));
2871         } else {
2872                 s = _select;
2873         }
2874         if (s) {
2875                 if (s->trim_control()) {
2876                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
2877                         return 0;
2878                 }
2879         }
2880         return sel_fail ("trimdB", 0, get_address (msg));
2881 }
2882
2883 int
2884 OSC::sel_pan_position (float val, lo_message msg)
2885 {
2886         OSCSurface *sur = get_surface(get_address (msg));
2887         boost::shared_ptr<Stripable> s;
2888         if (sur->expand_enable) {
2889                 s = get_strip (sur->expand, get_address (msg));
2890         } else {
2891                 s = _select;
2892         }
2893         if (s) {
2894                 if(s->pan_azimuth_control()) {
2895                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2896                         return 0;
2897                 }
2898         }
2899         return sel_fail ("pan_stereo_position", 0.5, get_address (msg));
2900 }
2901
2902 int
2903 OSC::sel_pan_width (float val, lo_message msg)
2904 {
2905         OSCSurface *sur = get_surface(get_address (msg));
2906         boost::shared_ptr<Stripable> s;
2907         if (sur->expand_enable) {
2908                 s = get_strip (sur->expand, get_address (msg));
2909         } else {
2910                 s = _select;
2911         }
2912         if (s) {
2913                 if (s->pan_width_control()) {
2914                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
2915                         return 0;
2916                 }
2917         }
2918         return sel_fail ("pan_stereo_width", 1, get_address (msg));
2919 }
2920
2921 int
2922 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
2923 {
2924         if (!session) return -1;
2925         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2926
2927         if (s) {
2928                 if(s->pan_azimuth_control()) {
2929                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), PBD::Controllable::NoGroup);
2930                         return 0;
2931                 }
2932         }
2933
2934         return route_send_fail ("pan_stereo_position", ssid, 0.5, get_address (msg));
2935 }
2936
2937 int
2938 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
2939 {
2940         if (!session) return -1;
2941         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2942
2943         if (s) {
2944                 if (s->pan_width_control()) {
2945                         s->pan_width_control()->set_value (pos, PBD::Controllable::NoGroup);
2946                         return 0;
2947                 }
2948         }
2949
2950         return route_send_fail ("pan_stereo_width", ssid, 1, get_address (msg));
2951 }
2952
2953 int
2954 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
2955 {
2956         if (!session) {
2957                 return -1;
2958         }
2959         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2960         float abs;
2961         if (s) {
2962                 if (id > 0) {
2963                         --id;
2964                 }
2965 #ifdef MIXBUS
2966                 abs = val;
2967 #else
2968                 if (val < -192) {
2969                         abs = 0;
2970                 } else {
2971                         abs = dB_to_coefficient (val);
2972                 }
2973 #endif
2974                 if (s->send_level_controllable (id)) {
2975                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
2976                         return 0;
2977                 }
2978         }
2979         return 0;
2980 }
2981
2982 int
2983 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
2984 {
2985         if (!session) {
2986                 return -1;
2987         }
2988         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
2989         float abs;
2990         if (s) {
2991
2992                 if (id > 0) {
2993                         --id;
2994                 }
2995
2996                 if (s->send_level_controllable (id)) {
2997 #ifdef MIXBUS
2998                         abs = s->send_level_controllable(id)->interface_to_internal (val);
2999 #else
3000                         abs = slider_position_to_gain_with_max (val, 2.0);
3001 #endif
3002                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3003                         return 0;
3004                 }
3005         }
3006         return 0;
3007 }
3008
3009 int
3010 OSC::sel_sendgain (int id, float val, lo_message msg)
3011 {
3012         OSCSurface *sur = get_surface(get_address (msg));
3013         boost::shared_ptr<Stripable> s;
3014         if (sur->expand_enable) {
3015                 s = get_strip (sur->expand, get_address (msg));
3016         } else {
3017                 s = _select;
3018         }
3019         float abs;
3020         if (s) {
3021                 if (id > 0) {
3022                         --id;
3023                 }
3024 #ifdef MIXBUS
3025                 abs = val;
3026 #else
3027                 if (val < -192) {
3028                         abs = 0;
3029                 } else {
3030                         abs = dB_to_coefficient (val);
3031                 }
3032 #endif
3033                 if (s->send_level_controllable (id)) {
3034                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3035                         return 0;
3036                 }
3037         }
3038         return sel_send_fail ("send_gain", id + 1, -193, get_address (msg));
3039 }
3040
3041 int
3042 OSC::sel_sendfader (int id, float val, lo_message msg)
3043 {
3044         OSCSurface *sur = get_surface(get_address (msg));
3045         boost::shared_ptr<Stripable> s;
3046         if (sur->expand_enable) {
3047                 s = get_strip (sur->expand, get_address (msg));
3048         } else {
3049                 s = _select;
3050         }
3051         float abs;
3052         if (s) {
3053
3054                 if (id > 0) {
3055                         --id;
3056                 }
3057
3058                 if (s->send_level_controllable (id)) {
3059 #ifdef MIXBUS
3060                         abs = s->send_level_controllable(id)->interface_to_internal (val);
3061 #else
3062                         abs = slider_position_to_gain_with_max (val, 2.0);
3063 #endif
3064                         s->send_level_controllable (id)->set_value (abs, PBD::Controllable::NoGroup);
3065                         return 0;
3066                 }
3067         }
3068         return sel_send_fail ("send_fader", id, 0, get_address (msg));
3069 }
3070
3071 int
3072 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
3073 {
3074         if (!session) {
3075                 return -1;
3076         }
3077         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3078
3079         if (s) {
3080
3081                 /* revert to zero-based counting */
3082
3083                 if (sid > 0) {
3084                         --sid;
3085                 }
3086
3087                 if (s->send_enable_controllable (sid)) {
3088                         s->send_enable_controllable (sid)->set_value (val, PBD::Controllable::NoGroup);
3089                         return 0;
3090                 }
3091
3092                 if (s->send_level_controllable (sid)) {
3093                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3094                         if (!r) {
3095                                 return 0;
3096                         }
3097                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
3098                         if (snd) {
3099                                 if (val) {
3100                                         snd->activate();
3101                                 } else {
3102                                         snd->deactivate();
3103                                 }
3104                         }
3105                         return 0;
3106                 }
3107
3108         }
3109
3110         return -1;
3111 }
3112
3113 int
3114 OSC::sel_sendenable (int id, float val, lo_message msg)
3115 {
3116         OSCSurface *sur = get_surface(get_address (msg));
3117         boost::shared_ptr<Stripable> s;
3118         if (sur->expand_enable) {
3119                 s = get_strip (sur->expand, get_address (msg));
3120         } else {
3121                 s = _select;
3122         }
3123         if (s) {
3124                 if (id > 0) {
3125                         --id;
3126                 }
3127                 if (s->send_enable_controllable (id)) {
3128                         s->send_enable_controllable (id)->set_value (val, PBD::Controllable::NoGroup);
3129                         return 0;
3130                 }
3131                 if (s->send_level_controllable (id)) {
3132                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3133                         if (!r) {
3134                                 // should never get here
3135                                 return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3136                         }
3137                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(id));
3138                         if (snd) {
3139                                 if (val) {
3140                                         snd->activate();
3141                                 } else {
3142                                         snd->deactivate();
3143                                 }
3144                         }
3145                         return 0;
3146                 }
3147         }
3148         return sel_send_fail ("send_enable", id + 1, 0, get_address (msg));
3149 }
3150
3151 int
3152 OSC::route_plugin_list (int ssid, lo_message msg) {
3153         if (!session) {
3154                 return -1;
3155         }
3156
3157         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3158
3159         if (!r) {
3160                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3161                 return -1;
3162         }
3163         int piid = 0;
3164
3165         lo_message reply = lo_message_new ();
3166         lo_message_add_int32 (reply, ssid);
3167
3168
3169         for (;;) {
3170                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
3171                 if ( !redi ) {
3172                         break;
3173                 }
3174
3175                 boost::shared_ptr<PluginInsert> pi;
3176
3177                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3178                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3179                         continue;
3180                 }
3181                 lo_message_add_int32 (reply, piid + 1);
3182
3183                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3184                 lo_message_add_string (reply, pip->name());
3185
3186                 piid++;
3187         }
3188
3189         lo_send_message (get_address (msg), "/strip/plugin/list", reply);
3190         lo_message_free (reply);
3191         return 0;
3192 }
3193
3194 int
3195 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
3196         if (!session) {
3197                 return -1;
3198         }
3199
3200         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3201
3202         if (!r) {
3203                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3204                 return -1;
3205         }
3206
3207         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3208
3209         if (!redi) {
3210                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3211                 return -1;
3212         }
3213
3214         boost::shared_ptr<PluginInsert> pi;
3215
3216         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3217                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3218                 return -1;
3219         }
3220
3221         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3222         bool ok = false;
3223
3224         lo_message reply = lo_message_new();
3225         lo_message_add_int32 (reply, ssid);
3226         lo_message_add_int32 (reply, piid);
3227         lo_message_add_string (reply, pip->name());
3228         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
3229
3230                 uint32_t controlid = pip->nth_parameter(ppi, ok);
3231                 if (!ok) {
3232                         continue;
3233                 }
3234                 if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
3235                         boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
3236
3237                                 lo_message_add_int32 (reply, ppi + 1);
3238                                 ParameterDescriptor pd;
3239                                 pi->plugin()->get_parameter_descriptor(controlid, pd);
3240                                 lo_message_add_string (reply, pd.label.c_str());
3241
3242                                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
3243                                 int flags = 0;
3244                                 flags |= pd.enumeration ? 1 : 0;
3245                                 flags |= pd.integer_step ? 2 : 0;
3246                                 flags |= pd.logarithmic ? 4 : 0;
3247                                 flags |= pd.max_unbound ? 8 : 0;
3248                                 flags |= pd.min_unbound ? 16 : 0;
3249                                 flags |= pd.sr_dependent ? 32 : 0;
3250                                 flags |= pd.toggled ? 64 : 0;
3251                                 flags |= c != NULL ? 128 : 0; // bit 7 indicates in input control
3252                                 lo_message_add_int32 (reply, flags);
3253
3254                                 lo_message_add_int32 (reply, pd.datatype);
3255                                 lo_message_add_float (reply, pd.lower);
3256                                 lo_message_add_float (reply, pd.upper);
3257                                 lo_message_add_string (reply, pd.print_fmt.c_str());
3258                                 if ( pd.scale_points ) {
3259                                         lo_message_add_int32 (reply, pd.scale_points->size());
3260                                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
3261                                                 lo_message_add_int32 (reply, i->second);
3262                                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
3263                                         }
3264                                 }
3265                                 else {
3266                                         lo_message_add_int32 (reply, 0);
3267                                 }
3268                                 if ( c ) {
3269                                         lo_message_add_double (reply, c->get_value());
3270                                 }
3271                                 else {
3272                                         lo_message_add_double (reply, 0);
3273                         }
3274                 }
3275         }
3276
3277         lo_send_message (get_address (msg), "/strip/plugin/descriptor", reply);
3278         lo_message_free (reply);
3279
3280         return 0;
3281 }
3282
3283 int
3284 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
3285         if (!session) {
3286                 return -1;
3287         }
3288
3289         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
3290
3291         if (!r) {
3292                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3293                 return -1;
3294         }
3295
3296         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
3297
3298         if (!redi) {
3299                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3300                 return -1;
3301         }
3302
3303         boost::shared_ptr<PluginInsert> pi;
3304
3305         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3306                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3307                 return -1;
3308         }
3309
3310         pi->reset_parameters_to_default ();
3311
3312         return 0;
3313 }
3314
3315 int
3316 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
3317 {
3318         if (!session)
3319                 return -1;
3320         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3321
3322         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3323
3324         if (!r) {
3325                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3326                 return -1;
3327         }
3328
3329         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3330
3331         if (!redi) {
3332                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3333                 return -1;
3334         }
3335
3336         boost::shared_ptr<PluginInsert> pi;
3337
3338         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3339                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3340                 return -1;
3341         }
3342
3343         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3344         bool ok=false;
3345
3346         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3347
3348         if (!ok) {
3349                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
3350                 return -1;
3351         }
3352
3353         if (!pip->parameter_is_input(controlid)) {
3354                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
3355                 return -1;
3356         }
3357
3358         ParameterDescriptor pd;
3359         pi->plugin()->get_parameter_descriptor (controlid,pd);
3360
3361         if (val >= pd.lower && val <= pd.upper) {
3362
3363                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3364                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
3365                 c->set_value (val, PBD::Controllable::NoGroup);
3366         } else {
3367                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
3368                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
3369         }
3370
3371         return 0;
3372 }
3373
3374 //prints to cerr only
3375 int
3376 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
3377 {
3378         if (!session) {
3379                 return -1;
3380         }
3381         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3382
3383         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3384
3385         if (!r) {
3386                 return -1;
3387         }
3388
3389         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3390
3391         if (!redi) {
3392                 return -1;
3393         }
3394
3395         boost::shared_ptr<PluginInsert> pi;
3396
3397         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3398                 return -1;
3399         }
3400
3401         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3402         bool ok=false;
3403
3404         uint32_t controlid = pip->nth_parameter (par - 1,ok);
3405
3406         if (!ok) {
3407                 return -1;
3408         }
3409
3410         ParameterDescriptor pd;
3411
3412         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
3413                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
3414
3415                 cerr << "parameter:     " << pd.label  << "\n";
3416                 if (c) {
3417                         cerr << "current value: " << c->get_value () << "\n";
3418                 } else {
3419                         cerr << "current value not available, control does not exist\n";
3420                 }
3421                 cerr << "lower value:   " << pd.lower << "\n";
3422                 cerr << "upper value:   " << pd.upper << "\n";
3423         }
3424
3425         return 0;
3426 }
3427
3428 int
3429 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
3430 {
3431         if (!session)
3432                 return -1;
3433         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3434
3435         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3436
3437         if (!r) {
3438                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3439                 return -1;
3440         }
3441
3442         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3443
3444         if (!redi) {
3445                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3446                 return -1;
3447         }
3448
3449         boost::shared_ptr<PluginInsert> pi;
3450
3451         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3452                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3453                 return -1;
3454         }
3455
3456         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3457         pi->activate();
3458
3459         return 0;
3460 }
3461
3462 int
3463 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
3464 {
3465         if (!session)
3466                 return -1;
3467         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3468
3469         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
3470
3471         if (!r) {
3472                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
3473                 return -1;
3474         }
3475
3476         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
3477
3478         if (!redi) {
3479                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
3480                 return -1;
3481         }
3482
3483         boost::shared_ptr<PluginInsert> pi;
3484
3485         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
3486                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
3487                 return -1;
3488         }
3489
3490         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
3491         pi->deactivate();
3492
3493         return 0;
3494 }
3495
3496 // select
3497
3498 int
3499 OSC::sel_pan_elevation (float val, lo_message msg)
3500 {
3501         OSCSurface *sur = get_surface(get_address (msg));
3502         boost::shared_ptr<Stripable> s;
3503         if (sur->expand_enable) {
3504                 s = get_strip (sur->expand, get_address (msg));
3505         } else {
3506                 s = _select;
3507         }
3508         if (s) {
3509                 if (s->pan_elevation_control()) {
3510                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3511                         return 0;
3512                 }
3513         }
3514         return sel_fail ("pan_elevation_position", 0, get_address (msg));
3515 }
3516
3517 int
3518 OSC::sel_pan_frontback (float val, lo_message msg)
3519 {
3520         OSCSurface *sur = get_surface(get_address (msg));
3521         boost::shared_ptr<Stripable> s;
3522         if (sur->expand_enable) {
3523                 s = get_strip (sur->expand, get_address (msg));
3524         } else {
3525                 s = _select;
3526         }
3527         if (s) {
3528                 if (s->pan_frontback_control()) {
3529                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3530                         return 0;
3531                 }
3532         }
3533         return sel_fail ("pan_frontback_position", 0.5, get_address (msg));
3534 }
3535
3536 int
3537 OSC::sel_pan_lfe (float val, lo_message msg)
3538 {
3539         OSCSurface *sur = get_surface(get_address (msg));
3540         boost::shared_ptr<Stripable> s;
3541         if (sur->expand_enable) {
3542                 s = get_strip (sur->expand, get_address (msg));
3543         } else {
3544                 s = _select;
3545         }
3546         if (s) {
3547                 if (s->pan_lfe_control()) {
3548                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
3549                         return 0;
3550                 }
3551         }
3552         return sel_fail ("pan_lfe_control", 0, get_address (msg));
3553 }
3554
3555 // compressor control
3556 int
3557 OSC::sel_comp_enable (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->comp_enable_controllable()) {
3568                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3569                         return 0;
3570                 }
3571         }
3572         return sel_fail ("comp_enable", 0, get_address (msg));
3573 }
3574
3575 int
3576 OSC::sel_comp_threshold (float val, lo_message msg)
3577 {
3578         OSCSurface *sur = get_surface(get_address (msg));
3579         boost::shared_ptr<Stripable> s;
3580         if (sur->expand_enable) {
3581                 s = get_strip (sur->expand, get_address (msg));
3582         } else {
3583                 s = _select;
3584         }
3585         if (s) {
3586                 if (s->comp_threshold_controllable()) {
3587                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3588                         return 0;
3589                 }
3590         }
3591         return sel_fail ("comp_threshold", 0, get_address (msg));
3592 }
3593
3594 int
3595 OSC::sel_comp_speed (float val, lo_message msg)
3596 {
3597         OSCSurface *sur = get_surface(get_address (msg));
3598         boost::shared_ptr<Stripable> s;
3599         if (sur->expand_enable) {
3600                 s = get_strip (sur->expand, get_address (msg));
3601         } else {
3602                 s = _select;
3603         }
3604         if (s) {
3605                 if (s->comp_speed_controllable()) {
3606                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3607                         return 0;
3608                 }
3609         }
3610         return sel_fail ("comp_speed", 0, get_address (msg));
3611 }
3612
3613 int
3614 OSC::sel_comp_mode (float val, lo_message msg)
3615 {
3616         OSCSurface *sur = get_surface(get_address (msg));
3617         boost::shared_ptr<Stripable> s;
3618         if (sur->expand_enable) {
3619                 s = get_strip (sur->expand, get_address (msg));
3620         } else {
3621                 s = _select;
3622         }
3623         if (s) {
3624                 if (s->comp_mode_controllable()) {
3625                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3626                         return 0;
3627                 }
3628         }
3629         return sel_fail ("comp_mode", 0, get_address (msg));
3630 }
3631
3632 int
3633 OSC::sel_comp_makeup (float val, lo_message msg)
3634 {
3635         OSCSurface *sur = get_surface(get_address (msg));
3636         boost::shared_ptr<Stripable> s;
3637         if (sur->expand_enable) {
3638                 s = get_strip (sur->expand, get_address (msg));
3639         } else {
3640                 s = _select;
3641         }
3642         if (s) {
3643                 if (s->comp_makeup_controllable()) {
3644                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3645                         return 0;
3646                 }
3647         }
3648         return sel_fail ("comp_makeup", 0, get_address (msg));
3649 }
3650
3651 // EQ control
3652
3653 int
3654 OSC::sel_eq_enable (float val, lo_message msg)
3655 {
3656         OSCSurface *sur = get_surface(get_address (msg));
3657         boost::shared_ptr<Stripable> s;
3658         if (sur->expand_enable) {
3659                 s = get_strip (sur->expand, get_address (msg));
3660         } else {
3661                 s = _select;
3662         }
3663         if (s) {
3664                 if (s->eq_enable_controllable()) {
3665                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3666                         return 0;
3667                 }
3668         }
3669         return sel_fail ("eq_enable", 0, get_address (msg));
3670 }
3671
3672 int
3673 OSC::sel_eq_hpf (float val, lo_message msg)
3674 {
3675         OSCSurface *sur = get_surface(get_address (msg));
3676         boost::shared_ptr<Stripable> s;
3677         if (sur->expand_enable) {
3678                 s = get_strip (sur->expand, get_address (msg));
3679         } else {
3680                 s = _select;
3681         }
3682         if (s) {
3683                 if (s->eq_hpf_controllable()) {
3684                         s->eq_hpf_controllable()->set_value (s->eq_hpf_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
3685                         return 0;
3686                 }
3687         }
3688         return sel_fail ("eq_hpf", 0, get_address (msg));
3689 }
3690
3691 int
3692 OSC::sel_eq_gain (int id, float val, lo_message msg)
3693 {
3694         OSCSurface *sur = get_surface(get_address (msg));
3695         boost::shared_ptr<Stripable> s;
3696         if (sur->expand_enable) {
3697                 s = get_strip (sur->expand, get_address (msg));
3698         } else {
3699                 s = _select;
3700         }
3701         if (s) {
3702                 if (id > 0) {
3703                         --id;
3704                 }
3705                 if (s->eq_gain_controllable (id)) {
3706                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3707                         return 0;
3708                 }
3709         }
3710         return sel_send_fail ("eq_gain", id + 1, 0, get_address (msg));
3711 }
3712
3713 int
3714 OSC::sel_eq_freq (int id, float val, lo_message msg)
3715 {
3716         OSCSurface *sur = get_surface(get_address (msg));
3717         boost::shared_ptr<Stripable> s;
3718         if (sur->expand_enable) {
3719                 s = get_strip (sur->expand, get_address (msg));
3720         } else {
3721                 s = _select;
3722         }
3723         if (s) {
3724                 if (id > 0) {
3725                         --id;
3726                 }
3727                 if (s->eq_freq_controllable (id)) {
3728                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3729                         return 0;
3730                 }
3731         }
3732         return sel_send_fail ("eq_freq", id + 1, 0, get_address (msg));
3733 }
3734
3735 int
3736 OSC::sel_eq_q (int id, float val, lo_message msg)
3737 {
3738         OSCSurface *sur = get_surface(get_address (msg));
3739         boost::shared_ptr<Stripable> s;
3740         if (sur->expand_enable) {
3741                 s = get_strip (sur->expand, get_address (msg));
3742         } else {
3743                 s = _select;
3744         }
3745         if (s) {
3746                 if (id > 0) {
3747                         --id;
3748                 }
3749                 if (s->eq_q_controllable (id)) {
3750                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3751                         return 0;
3752                 }
3753         }
3754         return sel_send_fail ("eq_q", id + 1, 0, get_address (msg));
3755 }
3756
3757 int
3758 OSC::sel_eq_shape (int id, float val, lo_message msg)
3759 {
3760         OSCSurface *sur = get_surface(get_address (msg));
3761         boost::shared_ptr<Stripable> s;
3762         if (sur->expand_enable) {
3763                 s = get_strip (sur->expand, get_address (msg));
3764         } else {
3765                 s = _select;
3766         }
3767         if (s) {
3768                 if (id > 0) {
3769                         --id;
3770                 }
3771                 if (s->eq_shape_controllable (id)) {
3772                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
3773                         return 0;
3774                 }
3775         }
3776         return sel_send_fail ("eq_shape", id + 1, 0, get_address (msg));
3777 }
3778
3779 void
3780 OSC::gui_selection_changed ()
3781 {
3782         boost::shared_ptr<Stripable> strip = ControlProtocol::first_selected_stripable();
3783
3784         if (strip) {
3785                 _select = strip;
3786                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3787                         OSCSurface* sur = &_surface[it];
3788                         if(!sur->expand_enable) {
3789                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3790                                 _strip_select (strip, addr);
3791                         }
3792                 }
3793         }
3794 }
3795
3796 // timer callbacks
3797 bool
3798 OSC::periodic (void)
3799 {
3800         if (!tick) {
3801                 Glib::usleep(100); // let flurry of signals subside
3802                 if (global_init) {
3803                         for (uint32_t it = 0; it < _surface.size(); it++) {
3804                                 OSCSurface* sur = &_surface[it];
3805                                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
3806                                 global_feedback (sur->feedback, addr, sur->gainmode);
3807                         }
3808                         global_init = false;
3809                         tick = true;
3810                 }
3811                 if (bank_dirty) {
3812                         _recalcbanks ();
3813                         bank_dirty = false;
3814                         tick = true;
3815                 }
3816         }
3817
3818         if (scrub_speed != 0) {
3819                 // for those jog wheels that don't have 0 on release (touch), time out.
3820                 int64_t now = ARDOUR::get_microseconds ();
3821                 int64_t diff = now - scrub_time;
3822                 if (diff > 120000) {
3823                         scrub_speed = 0;
3824                         session->request_transport_speed (0);
3825                         // locate to the place PH was at last tick
3826                         session->request_locate (scrub_place, false);
3827                 }
3828         }
3829
3830         for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
3831
3832                 OSCGlobalObserver* go;
3833
3834                 if ((go = dynamic_cast<OSCGlobalObserver*>(*x)) != 0) {
3835                         go->tick();
3836                 }
3837         }
3838         for (RouteObservers::iterator x = route_observers.begin(); x != route_observers.end(); x++) {
3839
3840                 OSCRouteObserver* ro;
3841
3842                 if ((ro = dynamic_cast<OSCRouteObserver*>(*x)) != 0) {
3843                         ro->tick();
3844                 }
3845         }
3846         for (uint32_t it = 0; it < _surface.size(); it++) {
3847                 OSCSurface* sur = &_surface[it];
3848                 OSCSelectObserver* so;
3849                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
3850                         so->tick();
3851                 }
3852         }
3853         for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end(); x++) {
3854
3855                 OSCCueObserver* co;
3856
3857                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
3858                         co->tick();
3859                 }
3860         }
3861         return true;
3862 }
3863
3864 int
3865 OSC::route_send_fail (string path, uint32_t ssid, float val, lo_address addr)
3866 {
3867         OSCSurface *sur = get_surface(addr);
3868
3869         ostringstream os;
3870         lo_message reply;
3871         if (ssid) {
3872                 reply = lo_message_new ();
3873                 if (sur->feedback[2]) {
3874                         os << "/strip/" << path << "/" << ssid;
3875                 } else {
3876                         os << "/strip/" << path;
3877                         lo_message_add_int32 (reply, ssid);
3878                 }
3879                 string str_pth = os.str();
3880                 lo_message_add_float (reply, (float) val);
3881
3882                 lo_send_message (addr, str_pth.c_str(), reply);
3883                 lo_message_free (reply);
3884         }
3885         if ((_select == get_strip (ssid, addr)) || ((sur->expand == ssid) && (sur->expand_enable))) {
3886                 os.str("");
3887                 os << "/select/" << path;
3888                 string sel_pth = os.str();
3889                 reply = lo_message_new ();
3890                 lo_message_add_float (reply, (float) val);
3891                 lo_send_message (addr, sel_pth.c_str(), reply);
3892                 lo_message_free (reply);
3893         }
3894
3895         return 0;
3896 }
3897
3898 int
3899 OSC::sel_fail (string path, float val, lo_address addr)
3900 {
3901         ostringstream os;
3902         os.str("");
3903         os << "/select/" << path;
3904         string sel_pth = os.str();
3905         lo_message reply = lo_message_new ();
3906         lo_message_add_float (reply, (float) val);
3907         lo_send_message (addr, sel_pth.c_str(), reply);
3908         lo_message_free (reply);
3909
3910         return 0;
3911 }
3912
3913 int
3914 OSC::sel_send_fail (string path, uint32_t id, float val, lo_address addr)
3915 {
3916         OSCSurface *sur = get_surface(addr);
3917
3918         ostringstream os;
3919         lo_message reply;
3920         reply = lo_message_new ();
3921         if (sur->feedback[2]) {
3922                 os << "/select/" << path << "/" << id;
3923         } else {
3924                 os << "/select/" << path;
3925                 lo_message_add_int32 (reply, id);
3926         }
3927         string str_pth = os.str();
3928         lo_message_add_float (reply, (float) val);
3929
3930         lo_send_message (addr, str_pth.c_str(), reply);
3931         lo_message_free (reply);
3932
3933         return 0;
3934 }
3935
3936 XMLNode&
3937 OSC::get_state ()
3938 {
3939         XMLNode& node (ControlProtocol::get_state());
3940         node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
3941         node.set_property ("address-only", address_only);
3942         node.set_property ("remote-port", remote_port);
3943         node.set_property ("banksize", default_banksize);
3944         node.set_property ("striptypes", default_strip);
3945         node.set_property ("feedback", default_feedback);
3946         node.set_property ("gainmode", default_gainmode);
3947         if (_surface.size()) {
3948                 XMLNode* config = new XMLNode (X_("Configurations"));
3949                 for (uint32_t it = 0; it < _surface.size(); ++it) {
3950                         OSCSurface* sur = &_surface[it];
3951                         XMLNode* devnode = new XMLNode (X_("Configuration"));
3952                         devnode->set_property (X_("url"), sur->remote_url);
3953                         devnode->set_property (X_("bank-size"), sur->bank_size);
3954                         devnode->set_property (X_("strip-types"), (uint64_t)sur->strip_types.to_ulong());
3955                         devnode->set_property (X_("feedback"), (uint64_t)sur->feedback.to_ulong());
3956                         devnode->set_property (X_("gainmode"), sur->gainmode);
3957                         config->add_child_nocopy (*devnode);
3958                 }
3959                 node.add_child_nocopy (*config);
3960         }
3961         return node;
3962 }
3963
3964 int
3965 OSC::set_state (const XMLNode& node, int version)
3966 {
3967         if (ControlProtocol::set_state (node, version)) {
3968                 return -1;
3969         }
3970         int32_t debugmode;
3971         if (node.get_property (X_("debugmode"), debugmode)) {
3972                 _debugmode = OSCDebugMode (debugmode);
3973         }
3974
3975         node.get_property (X_("address-only"), address_only);
3976         node.get_property (X_("remote-port"), remote_port);
3977         node.get_property (X_("banksize"), default_banksize);
3978         node.get_property (X_("striptypes"), default_strip);
3979         node.get_property (X_("feedback"), default_feedback);
3980         node.get_property (X_("gainmode"), default_gainmode);
3981
3982         XMLNode* cnode = node.child (X_("Configurations"));
3983
3984         if (cnode) {
3985                 XMLNodeList const& devices = cnode->children();
3986                 for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
3987                         OSCSurface s;
3988                         if (!(*d)->get_property (X_("url"), s.remote_url)) {
3989                                 continue;
3990                         }
3991
3992                         bank_dirty = true;
3993
3994                         (*d)->get_property (X_("bank-size"), s.bank_size);
3995
3996                         uint64_t bits;
3997                         if ((*d)->get_property (X_ ("strip-types"), bits)) {
3998                                 s.strip_types = bits;
3999                         }
4000                         if ((*d)->get_property (X_("feedback"), bits)) {
4001                                 s.feedback = bits;
4002                         }
4003                         (*d)->get_property (X_("gainmode"), s.gainmode);
4004
4005                         s.bank = 1;
4006                         s.sel_obs = 0;
4007                         s.expand = 0;
4008                         s.expand_enable = false;
4009                         s.strips = get_sorted_stripables (s.strip_types, s.cue);
4010                         s.nstrips = s.strips.size ();
4011                         _surface.push_back (s);
4012                 }
4013         }
4014         global_init = true;
4015         tick = false;
4016
4017         return 0;
4018 }
4019
4020 // predicate for sort call in get_sorted_stripables
4021 struct StripableByPresentationOrder
4022 {
4023         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
4024         {
4025                 return a->presentation_info().order() < b->presentation_info().order();
4026         }
4027
4028         bool operator () (const Stripable & a, const Stripable & b) const
4029         {
4030                 return a.presentation_info().order() < b.presentation_info().order();
4031         }
4032
4033         bool operator () (const Stripable * a, const Stripable * b) const
4034         {
4035                 return a->presentation_info().order() < b->presentation_info().order();
4036         }
4037 };
4038
4039 OSC::Sorted
4040 OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
4041 {
4042         Sorted sorted;
4043
4044         // fetch all stripables
4045         StripableList stripables;
4046
4047         session->get_stripables (stripables);
4048
4049         // Look for stripables that match bit in sur->strip_types
4050         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4051
4052                 boost::shared_ptr<Stripable> s = *it;
4053                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4054                         // do nothing... skip it
4055                 } else {
4056
4057                         if (types[0] && (s->presentation_info().flags() & PresentationInfo::AudioTrack)) {
4058                                 sorted.push_back (s);
4059                         } else
4060                         if (types[1] && (s->presentation_info().flags() & PresentationInfo::MidiTrack)) {
4061                                 sorted.push_back (s);
4062                         } else
4063                         if ((s->presentation_info().flags() & PresentationInfo::AudioBus)) {
4064                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4065                                 // r->feeds (session->master_out()) may make more sense
4066                                 if (r->direct_feeds_according_to_reality (session->master_out())) {
4067                                         // this is a bus
4068                                         if (types[2]) {
4069                                                 sorted.push_back (s);
4070                                         }
4071                                 } else {
4072                                         // this is an Aux out
4073                                         if (types[7]) {
4074                                                 sorted.push_back (s);
4075                                         }
4076                                 }
4077                         } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
4078                                 sorted.push_back (s);
4079                         } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
4080                                 sorted.push_back (s);
4081                         } else if (types[8] && (s->is_selected())) {
4082                                 sorted.push_back (s);
4083                         } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
4084                                 sorted.push_back (s);
4085                         }
4086                 }
4087         }
4088         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4089         // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
4090         if (types[5]) {
4091                 sorted.push_back (session->master_out());
4092         }
4093         if (types[6]) {
4094                 sorted.push_back (session->monitor_out());
4095         }
4096         return sorted;
4097 }
4098
4099 int
4100 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
4101 {
4102         int ret = 1; /* unhandled */
4103
4104         if (!strncmp (path, "/cue/aux", 8)) {
4105                 // set our Aux bus
4106                 ret = cue_set (argv[0]->i, msg);
4107         }
4108         else if (!strncmp (path, "/cue/connect", 12)) {
4109                 // Connect to default Aux bus
4110                 if ((!argc) || argv[0]->i) {
4111                         ret = cue_set (1, msg);
4112                 }
4113         }
4114         else if (!strncmp (path, "/cue/next_aux", 13)) {
4115                 // switch to next Aux bus
4116                 if ((!argc) || argv[0]->i) {
4117                         ret = cue_next (msg);
4118                 }
4119         }
4120         else if (!strncmp (path, "/cue/previous_aux", 17)) {
4121                 // switch to previous Aux bus
4122                 if ((!argc) || argv[0]->i) {
4123                         ret = cue_previous (msg);
4124                 }
4125         }
4126         else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
4127                 int id = atoi (&path[16]);
4128                 ret = cue_send_fader (id, argv[0]->f, msg);
4129         }
4130         else if (!strncmp (path, "/cue/send/enable/", 17) && strlen (path) > 17) {
4131                 int id = atoi (&path[17]);
4132                 ret = cue_send_enable (id, argv[0]->f, msg);
4133         }
4134         else if (!strncmp (path, "/cue/fader", 10)) {
4135                 ret = cue_aux_fader (argv[0]->f, msg);
4136         }
4137         else if (!strncmp (path, "/cue/mute", 9)) {
4138                 ret = cue_aux_mute (argv[0]->f, msg);
4139         }
4140
4141         return ret;
4142 }
4143
4144 int
4145 OSC::cue_set (uint32_t aux, lo_message msg)
4146 {
4147         return _cue_set (aux, get_address (msg));
4148 }
4149
4150 int
4151 OSC::_cue_set (uint32_t aux, lo_address addr)
4152 {
4153         int ret = 1;
4154         OSCSurface *s = get_surface(addr);
4155         s->bank_size = 0;
4156         s->strip_types = 128;
4157         s->feedback = 0;
4158         s->gainmode = 1;
4159         s->cue = true;
4160         s->strips = get_sorted_stripables(s->strip_types, s->cue);
4161
4162         s->nstrips = s->strips.size();
4163
4164         if (aux < 1) {
4165                 aux = 1;
4166         } else if (aux > s->nstrips) {
4167                 aux = s->nstrips;
4168         }
4169         s->aux = aux;
4170
4171         // get rid of any old CueObsevers for this address
4172         //cueobserver_connections.drop_connections ();
4173         CueObservers::iterator x;
4174         for (x = cue_observers.begin(); x != cue_observers.end();) {
4175
4176                 OSCCueObserver* co;
4177
4178                 if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
4179
4180                         int res = strcmp(lo_address_get_url(co->address()), lo_address_get_url(addr));
4181
4182                         if (res == 0) {
4183                                 delete *x;
4184                                 x = cue_observers.erase (x);
4185                         } else {
4186                                 ++x;
4187                         }
4188                 } else {
4189                         ++x;
4190                 }
4191         }
4192
4193         // get a list of Auxes
4194         for (uint32_t n = 0; n < s->nstrips; ++n) {
4195                 boost::shared_ptr<Stripable> stp = s->strips[n];
4196                 if (stp) {
4197                         text_message (string_compose ("/cue/name/%1", n+1), stp->name(), addr);
4198                         if (aux == n+1) {
4199                                 // aux must be at least one
4200                                 // need a signal if aux vanishes
4201                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
4202
4203                                 // make a list of stripables with sends that go to this bus
4204                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
4205                                 // start cue observer
4206                                 OSCCueObserver* co = new OSCCueObserver (stp, s->sends, addr);
4207                                 cue_observers.push_back (co);
4208                                 ret = 0;
4209                         }
4210
4211                 }
4212         }
4213
4214         return ret;
4215 }
4216
4217 int
4218 OSC::cue_next (lo_message msg)
4219 {
4220         OSCSurface *s = get_surface(get_address (msg));
4221         int ret = 1;
4222
4223         if (!s->cue) {
4224                 ret = cue_set (1, msg);
4225         }
4226         if (s->aux < s->nstrips) {
4227                 ret = cue_set (s->aux + 1, msg);
4228         } else {
4229                 ret = cue_set (s->nstrips, msg);
4230         }
4231         return ret;
4232 }
4233
4234 int
4235 OSC::cue_previous (lo_message msg)
4236 {
4237         OSCSurface *s = get_surface(get_address (msg));
4238         int ret = 1;
4239         if (!s->cue) {
4240                 ret = cue_set (1, msg);
4241         }
4242         if (s->aux > 1) {
4243                 ret = cue_set (s->aux - 1, msg);
4244         }
4245         return ret;
4246 }
4247
4248 boost::shared_ptr<Send>
4249 OSC::cue_get_send (uint32_t id, lo_address addr)
4250 {
4251         OSCSurface *s = get_surface(addr);
4252         if (id && s->aux > 0 && id <= s->sends.size()) {
4253                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
4254                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
4255                 if (r && aux) {
4256                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4257                 }
4258         }
4259         return boost::shared_ptr<Send>();
4260
4261 }
4262
4263 int
4264 OSC::cue_aux_fader (float position, lo_message msg)
4265 {
4266         if (!session) return -1;
4267
4268         OSCSurface *sur = get_surface(get_address (msg));
4269         if (sur->cue) {
4270                 if (sur->aux) {
4271                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4272
4273                         if (s) {
4274                                 float abs;
4275                                 abs = slider_position_to_gain_with_max (position, 2.0);
4276                                 if (s->gain_control()) {
4277                                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4278                                         return 0;
4279                                 }
4280                         }
4281                 }
4282         }
4283         cue_float_message ("/cue/fader", 0, get_address (msg));
4284         return -1;
4285 }
4286
4287 int
4288 OSC::cue_aux_mute (float state, lo_message msg)
4289 {
4290         if (!session) return -1;
4291
4292         OSCSurface *sur = get_surface(get_address (msg));
4293         if (sur->cue) {
4294                 if (sur->aux) {
4295                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
4296                         if (s) {
4297                                 if (s->mute_control()) {
4298                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
4299                                         return 0;
4300                                 }
4301                         }
4302                 }
4303         }
4304         cue_float_message ("/cue/mute", 0, get_address (msg));
4305         return -1;
4306 }
4307
4308 int
4309 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
4310 {
4311         if (!session) {
4312                 return -1;
4313         }
4314         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4315         float abs;
4316         if (s) {
4317                 if (s->gain_control()) {
4318                         abs = slider_position_to_gain_with_max (val, 2.0);
4319                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4320                         return 0;
4321                 }
4322         }
4323         cue_float_message (string_compose ("/cue/send/fader/%1", id), 0, get_address (msg));
4324         return -1;
4325 }
4326
4327 int
4328 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
4329 {
4330         if (!session)
4331                 return -1;
4332         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
4333         if (s) {
4334                 if (state) {
4335                         s->activate ();
4336                 } else {
4337                         s->deactivate ();
4338                 }
4339                 return 0;
4340         }
4341         cue_float_message (string_compose ("/cue/send/enable/%1", id), 0, get_address (msg));
4342         return -1;
4343 }
4344
4345 int
4346 OSC::cue_float_message (string path, float val, lo_address addr)
4347 {
4348
4349         lo_message reply;
4350         reply = lo_message_new ();
4351         lo_message_add_float (reply, (float) val);
4352
4353         lo_send_message (addr, path.c_str(), reply);
4354         lo_message_free (reply);
4355
4356         return 0;
4357 }
4358
4359 int
4360 OSC::text_message (string path, string val, lo_address addr)
4361 {
4362
4363         lo_message reply;
4364         reply = lo_message_new ();
4365         lo_message_add_string (reply, val.c_str());
4366
4367         lo_send_message (addr, path.c_str(), reply);
4368         lo_message_free (reply);
4369
4370         return 0;
4371 }
4372
4373
4374 // we have to have a sorted list of stripables that have sends pointed at our aux
4375 // we can use the one in osc.cc to get an aux list
4376 OSC::Sorted
4377 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
4378 {
4379         Sorted sorted;
4380         cueobserver_connections.drop_connections ();
4381         // fetch all stripables
4382         StripableList stripables;
4383
4384         session->get_stripables (stripables);
4385
4386         // Look for stripables that have a send to aux
4387         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
4388
4389                 boost::shared_ptr<Stripable> s = *it;
4390                 // we only want routes
4391                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4392                 if (r) {
4393                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
4394                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
4395                         if (snd) { // test for send to aux
4396                                 sorted.push_back (s);
4397                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
4398                         }
4399                 }
4400
4401
4402         }
4403         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
4404
4405         return sorted;
4406 }
4407