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