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