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