6612b0da50152ac51a7a99d28f5c853f4a90991c
[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/control_math.h"
32 #include <pbd/convert.h>
33 #include <pbd/pthread_utils.h>
34 #include <pbd/file_utils.h>
35 #include <pbd/failed_constructor.h>
36
37 #include "ardour/amp.h"
38 #include "ardour/session.h"
39 #include "ardour/route.h"
40 #include "ardour/route_group.h"
41 #include "ardour/audio_track.h"
42 #include "ardour/midi_track.h"
43 #include "ardour/vca.h"
44 #include "ardour/monitor_control.h"
45 #include "ardour/dB.h"
46 #include "ardour/filesystem_paths.h"
47 #include "ardour/panner.h"
48 #include "ardour/plugin.h"
49 #include "ardour/plugin_insert.h"
50 #include "ardour/presentation_info.h"
51 #include "ardour/profile.h"
52 #include "ardour/send.h"
53 #include "ardour/internal_send.h"
54 #include "ardour/phase_control.h"
55 #include "ardour/solo_isolate_control.h"
56 #include "ardour/solo_safe_control.h"
57 #include "ardour/vca_manager.h"
58
59 #include "osc_select_observer.h"
60 #include "osc.h"
61 #include "osc_controllable.h"
62 #include "osc_route_observer.h"
63 #include "osc_global_observer.h"
64 #include "osc_cue_observer.h"
65 #include "pbd/i18n.h"
66
67 using namespace ARDOUR;
68 using namespace std;
69 using namespace Glib;
70 using namespace ArdourSurface;
71
72 #include "pbd/abstract_ui.cc" // instantiate template
73
74 OSC* OSC::_instance = 0;
75
76 #ifdef DEBUG
77 static void error_callback(int num, const char *m, const char *path)
78 {
79         fprintf(stderr, "liblo server error %d in path %s: %s\n", num, path, m);
80 }
81 #else
82 static void error_callback(int, const char *, const char *)
83 {
84
85 }
86 #endif
87
88 OSC::OSC (Session& s, uint32_t port)
89         : ControlProtocol (s, X_("Open Sound Control (OSC)"))
90         , AbstractUI<OSCUIRequest> (name())
91         , local_server (0)
92         , remote_server (0)
93         , _port(port)
94         , _ok (true)
95         , _shutdown (false)
96         , _osc_server (0)
97         , _osc_unix_server (0)
98         , _debugmode (Off)
99         , address_only (true)
100         , remote_port ("8000")
101         , default_banksize (0)
102         , default_strip (159)
103         , default_feedback (0)
104         , default_gainmode (0)
105         , default_send_size (0)
106         , default_plugin_size (0)
107         , tick (true)
108         , bank_dirty (false)
109         , observer_busy (true)
110         , scrub_speed (0)
111         , gui (0)
112 {
113         _instance = this;
114
115         session->Exported.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::session_exported, this, _1, _2), this);
116 }
117
118 OSC::~OSC()
119 {
120         tick = false;
121         stop ();
122         tear_down_gui ();
123         _instance = 0;
124 }
125
126 void*
127 OSC::request_factory (uint32_t num_requests)
128 {
129         /* AbstractUI<T>::request_buffer_factory() is a template method only
130            instantiated in this source module. To provide something visible for
131            use in the interface/descriptor, we have this static method that is
132            template-free.
133         */
134         return request_buffer_factory (num_requests);
135 }
136
137 void
138 OSC::do_request (OSCUIRequest* req)
139 {
140         if (req->type == CallSlot) {
141
142                 call_slot (MISSING_INVALIDATOR, req->the_slot);
143
144         } else if (req->type == Quit) {
145
146                 stop ();
147         }
148 }
149
150 int
151 OSC::set_active (bool yn)
152 {
153         if (yn != active()) {
154
155                 if (yn) {
156                         if (start ()) {
157                                 return -1;
158                         }
159                 } else {
160                         if (stop ()) {
161                                 return -1;
162                         }
163                 }
164
165         }
166
167         return ControlProtocol::set_active (yn);
168 }
169
170 bool
171 OSC::get_active () const
172 {
173         return _osc_server != 0;
174 }
175
176 int
177 OSC::start ()
178 {
179         char tmpstr[255];
180
181         if (_osc_server) {
182                 /* already started */
183                 return 0;
184         }
185
186         for (int j=0; j < 20; ++j) {
187                 snprintf(tmpstr, sizeof(tmpstr), "%d", _port);
188
189                 //if ((_osc_server = lo_server_new_with_proto (tmpstr, LO_TCP, error_callback))) {
190                 //      break;
191                 //}
192
193                 if ((_osc_server = lo_server_new (tmpstr, error_callback))) {
194                         break;
195                 }
196
197 #ifdef DEBUG
198                 cerr << "can't get osc at port: " << _port << endl;
199 #endif
200                 _port++;
201                 continue;
202         }
203
204         if (!_osc_server) {
205                 return 1;
206         }
207
208 #ifdef ARDOUR_OSC_UNIX_SERVER
209
210         // APPEARS sluggish for now
211
212         // attempt to create unix socket server too
213
214         snprintf(tmpstr, sizeof(tmpstr), "/tmp/sooperlooper_XXXXXX");
215         int fd = mkstemp(tmpstr);
216
217         if (fd >= 0 ) {
218                 ::g_unlink (tmpstr);
219                 close (fd);
220
221                 _osc_unix_server = lo_server_new (tmpstr, error_callback);
222
223                 if (_osc_unix_server) {
224                         _osc_unix_socket_path = tmpstr;
225                 }
226         }
227 #endif
228
229         PBD::info << "OSC @ " << get_server_url () << endmsg;
230
231         std::string url_file;
232
233         if (find_file (ardour_config_search_path(), "osc_url", url_file)) {
234                 _osc_url_file = url_file;
235                 if (g_file_set_contents (_osc_url_file.c_str(), get_server_url().c_str(), -1, NULL)) {
236                         cerr << "Couldn't write '" <<  _osc_url_file << "'" <<endl;
237                 }
238         }
239
240         observer_busy = false;
241         register_callbacks();
242
243         session_loaded (*session);
244
245         // lo_server_thread_add_method(_sthread, NULL, NULL, OSC::_dummy_handler, this);
246
247         /* startup the event loop thread */
248
249         BaseUI::run ();
250
251         // start timers for metering, timecode and heartbeat.
252         // timecode and metering run at 100
253         Glib::RefPtr<Glib::TimeoutSource> periodic_timeout = Glib::TimeoutSource::create (100); // milliseconds
254         periodic_connection = periodic_timeout->connect (sigc::mem_fun (*this, &OSC::periodic));
255         periodic_timeout->attach (main_loop()->get_context());
256
257         // catch track reordering
258         // receive routes added
259         session->RouteAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_routes_added, this, _1), this);
260         // receive VCAs added
261         session->vca_manager().VCAAdded.connect(session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::notify_vca_added, this, _1), this);
262         // order changed
263         PresentationInfo::Change.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
264
265         _select = ControlProtocol::first_selected_stripable();
266         if(!_select) {
267                 _select = session->master_out ();
268         }
269
270         return 0;
271 }
272
273 void
274 OSC::thread_init ()
275 {
276         pthread_set_name (event_loop_name().c_str());
277
278         if (_osc_unix_server) {
279                 Glib::RefPtr<IOSource> src = IOSource::create (lo_server_get_socket_fd (_osc_unix_server), IO_IN|IO_HUP|IO_ERR);
280                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_unix_server));
281                 src->attach (_main_loop->get_context());
282                 local_server = src->gobj();
283                 g_source_ref (local_server);
284         }
285
286         if (_osc_server) {
287 #ifdef PLATFORM_WINDOWS
288                 Glib::RefPtr<IOChannel> chan = Glib::IOChannel::create_from_win32_socket (lo_server_get_socket_fd (_osc_server));
289                 Glib::RefPtr<IOSource> src  = IOSource::create (chan, IO_IN|IO_HUP|IO_ERR);
290 #else
291                 Glib::RefPtr<IOSource> src  = IOSource::create (lo_server_get_socket_fd (_osc_server), IO_IN|IO_HUP|IO_ERR);
292 #endif
293                 src->connect (sigc::bind (sigc::mem_fun (*this, &OSC::osc_input_handler), _osc_server));
294                 src->attach (_main_loop->get_context());
295                 remote_server = src->gobj();
296                 g_source_ref (remote_server);
297         }
298
299         PBD::notify_event_loops_about_thread_creation (pthread_self(), event_loop_name(), 2048);
300         SessionEvent::create_per_thread_pool (event_loop_name(), 128);
301 }
302
303 int
304 OSC::stop ()
305 {
306         periodic_connection.disconnect ();
307         session_connections.drop_connections ();
308
309         // clear surfaces
310         observer_busy = true;
311         for (uint32_t it = 0; it < _surface.size (); ++it) {
312                 OSCSurface* sur = &_surface[it];
313                 surface_destroy (sur);
314         }
315         _surface.clear();
316
317         /* stop main loop */
318         if (local_server) {
319                 g_source_destroy (local_server);
320                 g_source_unref (local_server);
321                 local_server = 0;
322         }
323
324         if (remote_server) {
325                 g_source_destroy (remote_server);
326                 g_source_unref (remote_server);
327                 remote_server = 0;
328         }
329
330         BaseUI::quit ();
331
332         if (_osc_server) {
333                 lo_server_free (_osc_server);
334                 _osc_server = 0;
335         }
336
337         if (_osc_unix_server) {
338                 lo_server_free (_osc_unix_server);
339                 _osc_unix_server = 0;
340         }
341
342         if (!_osc_unix_socket_path.empty()) {
343                 ::g_unlink (_osc_unix_socket_path.c_str());
344         }
345
346         if (!_osc_url_file.empty() ) {
347                 ::g_unlink (_osc_url_file.c_str() );
348         }
349
350         return 0;
351 }
352
353 void
354 OSC::surface_destroy (OSCSurface* sur)
355 {
356         OSCSelectObserver* so;
357         if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
358                 so->clear_observer ();
359                 delete so;
360                 sur->sel_obs = 0;
361                 PBD::ScopedConnection pc = sur->proc_connection;
362                 pc.disconnect ();
363         }
364
365         OSCCueObserver* co;
366         if ((co = dynamic_cast<OSCCueObserver*>(sur->cue_obs)) != 0) {
367                 delete co;
368                 sur->cue_obs = 0;
369                 sur->sends.clear ();
370         }
371
372         OSCGlobalObserver* go;
373         if ((go = dynamic_cast<OSCGlobalObserver*>(sur->global_obs)) != 0) {
374                 go->clear_observer ();
375                 delete go;
376                 sur->global_obs = 0;
377         }
378         uint32_t st_end = sur->observers.size ();
379
380         for (uint32_t i = 0; i < st_end; i++) {
381                 OSCRouteObserver* ro;
382                 if ((ro = dynamic_cast<OSCRouteObserver*>(sur->observers[i])) != 0) {
383                         ro->clear_strip ();
384                         delete ro;
385                         ro = 0;
386                 }
387         }
388         sur->observers.clear();
389 }
390
391
392 void
393 OSC::register_callbacks()
394 {
395         lo_server srvs[2];
396         lo_server serv;
397
398         srvs[0] = _osc_server;
399         srvs[1] = _osc_unix_server;
400
401         for (size_t i = 0; i < 2; ++i) {
402
403                 if (!srvs[i]) {
404                         continue;
405                 }
406
407                 serv = srvs[i];
408
409
410 #define REGISTER_CALLBACK(serv,path,types, function) lo_server_add_method (serv, path, types, OSC::_ ## function, this)
411
412                 // Some controls have optional "f" for feedback or touchosc
413                 // http://hexler.net/docs/touchosc-controls-reference
414
415                 REGISTER_CALLBACK (serv, X_("/refresh"), "", refresh_surface);
416                 REGISTER_CALLBACK (serv, X_("/refresh"), "f", refresh_surface);
417                 REGISTER_CALLBACK (serv, X_("/strip/list"), "", routes_list);
418                 REGISTER_CALLBACK (serv, X_("/strip/list"), "f", routes_list);
419                 REGISTER_CALLBACK (serv, X_("/group/list"), "", group_list);
420                 REGISTER_CALLBACK (serv, X_("/group/list"), "f", group_list);
421                 REGISTER_CALLBACK (serv, X_("/strip/custom/mode"), "f", custom_mode);
422                 REGISTER_CALLBACK (serv, X_("/strip/custom/clear"), "f", custom_clear);
423                 REGISTER_CALLBACK (serv, X_("/strip/custom/clear"), "", custom_clear);
424                 REGISTER_CALLBACK (serv, X_("/surface/list"), "", surface_list);
425                 REGISTER_CALLBACK (serv, X_("/surface/list"), "f", surface_list);
426                 REGISTER_CALLBACK (serv, X_("/add_marker"), "", add_marker);
427                 REGISTER_CALLBACK (serv, X_("/add_marker"), "f", add_marker);
428                 REGISTER_CALLBACK (serv, X_("/access_action"), "s", access_action);
429                 REGISTER_CALLBACK (serv, X_("/loop_toggle"), "", loop_toggle);
430                 REGISTER_CALLBACK (serv, X_("/loop_toggle"), "f", loop_toggle);
431                 REGISTER_CALLBACK (serv, X_("/loop_location"), "ii", loop_location);
432                 REGISTER_CALLBACK (serv, X_("/goto_start"), "", goto_start);
433                 REGISTER_CALLBACK (serv, X_("/goto_start"), "f", goto_start);
434                 REGISTER_CALLBACK (serv, X_("/goto_end"), "", goto_end);
435                 REGISTER_CALLBACK (serv, X_("/goto_end"), "f", goto_end);
436                 REGISTER_CALLBACK (serv, X_("/scrub"), "f", scrub);
437                 REGISTER_CALLBACK (serv, X_("/jog"), "f", jog);
438                 REGISTER_CALLBACK (serv, X_("/jog/mode"), "f", jog_mode);
439                 REGISTER_CALLBACK (serv, X_("/rewind"), "", rewind);
440                 REGISTER_CALLBACK (serv, X_("/rewind"), "f", rewind);
441                 REGISTER_CALLBACK (serv, X_("/ffwd"), "", ffwd);
442                 REGISTER_CALLBACK (serv, X_("/ffwd"), "f", ffwd);
443                 REGISTER_CALLBACK (serv, X_("/transport_stop"), "", transport_stop);
444                 REGISTER_CALLBACK (serv, X_("/transport_stop"), "f", transport_stop);
445                 REGISTER_CALLBACK (serv, X_("/transport_play"), "", transport_play);
446                 REGISTER_CALLBACK (serv, X_("/transport_play"), "f", transport_play);
447                 REGISTER_CALLBACK (serv, X_("/transport_frame"), "", transport_sample);
448                 REGISTER_CALLBACK (serv, X_("/transport_speed"), "", transport_speed);
449                 REGISTER_CALLBACK (serv, X_("/record_enabled"), "", record_enabled);
450                 REGISTER_CALLBACK (serv, X_("/set_transport_speed"), "f", set_transport_speed);
451                 // locate ii is position and bool roll
452                 REGISTER_CALLBACK (serv, X_("/locate"), "ii", locate);
453                 REGISTER_CALLBACK (serv, X_("/save_state"), "", save_state);
454                 REGISTER_CALLBACK (serv, X_("/save_state"), "f", save_state);
455                 REGISTER_CALLBACK (serv, X_("/prev_marker"), "", prev_marker);
456                 REGISTER_CALLBACK (serv, X_("/prev_marker"), "f", prev_marker);
457                 REGISTER_CALLBACK (serv, X_("/next_marker"), "", next_marker);
458                 REGISTER_CALLBACK (serv, X_("/next_marker"), "f", next_marker);
459                 REGISTER_CALLBACK (serv, X_("/undo"), "", undo);
460                 REGISTER_CALLBACK (serv, X_("/undo"), "f", undo);
461                 REGISTER_CALLBACK (serv, X_("/redo"), "", redo);
462                 REGISTER_CALLBACK (serv, X_("/redo"), "f", redo);
463                 REGISTER_CALLBACK (serv, X_("/toggle_punch_in"), "", toggle_punch_in);
464                 REGISTER_CALLBACK (serv, X_("/toggle_punch_in"), "f", toggle_punch_in);
465                 REGISTER_CALLBACK (serv, X_("/toggle_punch_out"), "", toggle_punch_out);
466                 REGISTER_CALLBACK (serv, X_("/toggle_punch_out"), "f", toggle_punch_out);
467                 REGISTER_CALLBACK (serv, X_("/rec_enable_toggle"), "", rec_enable_toggle);
468                 REGISTER_CALLBACK (serv, X_("/rec_enable_toggle"), "f", rec_enable_toggle);
469                 REGISTER_CALLBACK (serv, X_("/toggle_all_rec_enables"), "", toggle_all_rec_enables);
470                 REGISTER_CALLBACK (serv, X_("/toggle_all_rec_enables"), "f", toggle_all_rec_enables);
471                 REGISTER_CALLBACK (serv, X_("/all_tracks_rec_in"), "f", all_tracks_rec_in);
472                 REGISTER_CALLBACK (serv, X_("/all_tracks_rec_out"), "f", all_tracks_rec_out);
473                 REGISTER_CALLBACK (serv, X_("/cancel_all_solos"), "f", cancel_all_solos);
474                 REGISTER_CALLBACK (serv, X_("/remove_marker"), "", remove_marker_at_playhead);
475                 REGISTER_CALLBACK (serv, X_("/remove_marker"), "f", remove_marker_at_playhead);
476                 REGISTER_CALLBACK (serv, X_("/jump_bars"), "f", jump_by_bars);
477                 REGISTER_CALLBACK (serv, X_("/jump_seconds"), "f", jump_by_seconds);
478                 REGISTER_CALLBACK (serv, X_("/mark_in"), "", mark_in);
479                 REGISTER_CALLBACK (serv, X_("/mark_in"), "f", mark_in);
480                 REGISTER_CALLBACK (serv, X_("/mark_out"), "", mark_out);
481                 REGISTER_CALLBACK (serv, X_("/mark_out"), "f", mark_out);
482                 REGISTER_CALLBACK (serv, X_("/toggle_click"), "", toggle_click);
483                 REGISTER_CALLBACK (serv, X_("/toggle_click"), "f", toggle_click);
484                 REGISTER_CALLBACK (serv, X_("/click/level"), "f", click_level);
485                 REGISTER_CALLBACK (serv, X_("/midi_panic"), "", midi_panic);
486                 REGISTER_CALLBACK (serv, X_("/midi_panic"), "f", midi_panic);
487                 REGISTER_CALLBACK (serv, X_("/toggle_roll"), "", toggle_roll);
488                 REGISTER_CALLBACK (serv, X_("/toggle_roll"), "f", toggle_roll);
489                 REGISTER_CALLBACK (serv, X_("/stop_forget"), "", stop_forget);
490                 REGISTER_CALLBACK (serv, X_("/stop_forget"), "f", stop_forget);
491                 REGISTER_CALLBACK (serv, X_("/set_punch_range"), "", set_punch_range);
492                 REGISTER_CALLBACK (serv, X_("/set_punch_range"), "f", set_punch_range);
493                 REGISTER_CALLBACK (serv, X_("/set_loop_range"), "", set_loop_range);
494                 REGISTER_CALLBACK (serv, X_("/set_loop_range"), "f", set_loop_range);
495                 REGISTER_CALLBACK (serv, X_("/set_session_range"), "", set_session_range);
496                 REGISTER_CALLBACK (serv, X_("/set_session_range"), "f", set_session_range);
497                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_mute"), "", toggle_monitor_mute);
498                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_mute"), "f", toggle_monitor_mute);
499                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_dim"), "", toggle_monitor_dim);
500                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_dim"), "f", toggle_monitor_dim);
501                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_mono"), "", toggle_monitor_mono);
502                 REGISTER_CALLBACK (serv, X_("/toggle_monitor_mono"), "f", toggle_monitor_mono);
503                 REGISTER_CALLBACK (serv, X_("/quick_snapshot_switch"), "", quick_snapshot_switch);
504                 REGISTER_CALLBACK (serv, X_("/quick_snapshot_switch"), "f", quick_snapshot_switch);
505                 REGISTER_CALLBACK (serv, X_("/quick_snapshot_stay"), "", quick_snapshot_stay);
506                 REGISTER_CALLBACK (serv, X_("/quick_snapshot_stay"), "f", quick_snapshot_stay);
507                 REGISTER_CALLBACK (serv, X_("/fit_1_track"), "", fit_1_track);
508                 REGISTER_CALLBACK (serv, X_("/fit_1_track"), "f", fit_1_track);
509                 REGISTER_CALLBACK (serv, X_("/fit_2_tracks"), "", fit_2_tracks);
510                 REGISTER_CALLBACK (serv, X_("/fit_2_tracks"), "f", fit_2_tracks);
511                 REGISTER_CALLBACK (serv, X_("/fit_4_tracks"), "", fit_4_tracks);
512                 REGISTER_CALLBACK (serv, X_("/fit_4_tracks"), "f", fit_4_tracks);
513                 REGISTER_CALLBACK (serv, X_("/fit_8_tracks"), "", fit_8_tracks);
514                 REGISTER_CALLBACK (serv, X_("/fit_8_tracks"), "f", fit_8_tracks);
515                 REGISTER_CALLBACK (serv, X_("/fit_16_tracks"), "", fit_16_tracks);
516                 REGISTER_CALLBACK (serv, X_("/fit_16_tracks"), "f", fit_16_tracks);
517                 REGISTER_CALLBACK (serv, X_("/fit_32_tracks"), "", fit_32_tracks);
518                 REGISTER_CALLBACK (serv, X_("/fit_32_tracks"), "f", fit_32_tracks);
519                 REGISTER_CALLBACK (serv, X_("/fit_all_tracks"), "", fit_all_tracks);
520                 REGISTER_CALLBACK (serv, X_("/fit_all_tracks"), "f", fit_all_tracks);
521                 REGISTER_CALLBACK (serv, X_("/zoom_100_ms"), "", zoom_100_ms);
522                 REGISTER_CALLBACK (serv, X_("/zoom_100_ms"), "f", zoom_100_ms);
523                 REGISTER_CALLBACK (serv, X_("/zoom_1_sec"), "", zoom_1_sec);
524                 REGISTER_CALLBACK (serv, X_("/zoom_1_sec"), "f", zoom_1_sec);
525                 REGISTER_CALLBACK (serv, X_("/zoom_10_sec"), "", zoom_10_sec);
526                 REGISTER_CALLBACK (serv, X_("/zoom_10_sec"), "f", zoom_10_sec);
527                 REGISTER_CALLBACK (serv, X_("/zoom_1_min"), "", zoom_1_min);
528                 REGISTER_CALLBACK (serv, X_("/zoom_1_min"), "f", zoom_1_min);
529                 REGISTER_CALLBACK (serv, X_("/zoom_5_min"), "", zoom_5_min);
530                 REGISTER_CALLBACK (serv, X_("/zoom_5_min"), "f", zoom_5_min);
531                 REGISTER_CALLBACK (serv, X_("/zoom_10_min"), "", zoom_10_min);
532                 REGISTER_CALLBACK (serv, X_("/zoom_10_min"), "f", zoom_10_min);
533                 REGISTER_CALLBACK (serv, X_("/zoom_to_session"), "", zoom_to_session);
534                 REGISTER_CALLBACK (serv, X_("/zoom_to_session"), "f", zoom_to_session);
535                 REGISTER_CALLBACK (serv, X_("/temporal_zoom_in"), "f", temporal_zoom_in);
536                 REGISTER_CALLBACK (serv, X_("/temporal_zoom_in"), "", temporal_zoom_in);
537                 REGISTER_CALLBACK (serv, X_("/temporal_zoom_out"), "", temporal_zoom_out);
538                 REGISTER_CALLBACK (serv, X_("/temporal_zoom_out"), "f", temporal_zoom_out);
539                 REGISTER_CALLBACK (serv, X_("/scroll_up_1_track"), "f", scroll_up_1_track);
540                 REGISTER_CALLBACK (serv, X_("/scroll_up_1_track"), "", scroll_up_1_track);
541                 REGISTER_CALLBACK (serv, X_("/scroll_dn_1_track"), "f", scroll_dn_1_track);
542                 REGISTER_CALLBACK (serv, X_("/scroll_dn_1_track"), "", scroll_dn_1_track);
543                 REGISTER_CALLBACK (serv, X_("/scroll_up_1_page"), "f", scroll_up_1_page);
544                 REGISTER_CALLBACK (serv, X_("/scroll_up_1_page"), "", scroll_up_1_page);
545                 REGISTER_CALLBACK (serv, X_("/scroll_dn_1_page"), "f", scroll_dn_1_page);
546                 REGISTER_CALLBACK (serv, X_("/scroll_dn_1_page"), "", scroll_dn_1_page);
547                 REGISTER_CALLBACK (serv, X_("/bank_up"), "", bank_up);
548                 REGISTER_CALLBACK (serv, X_("/bank_up"), "f", bank_delta);
549                 REGISTER_CALLBACK (serv, X_("/bank_down"), "", bank_down);
550                 REGISTER_CALLBACK (serv, X_("/bank_down"), "f", bank_down);
551                 REGISTER_CALLBACK (serv, X_("/use_group"), "f", use_group);
552
553                 // controls for "special" strips
554                 REGISTER_CALLBACK (serv, X_("/master/gain"), "f", master_set_gain);
555                 REGISTER_CALLBACK (serv, X_("/master/fader"), "f", master_set_fader);
556                 REGISTER_CALLBACK (serv, X_("/master/db_delta"), "f", master_delta_gain);
557                 REGISTER_CALLBACK (serv, X_("/master/mute"), "i", master_set_mute);
558                 REGISTER_CALLBACK (serv, X_("/master/trimdB"), "f", master_set_trim);
559                 REGISTER_CALLBACK (serv, X_("/master/pan_stereo_position"), "f", master_set_pan_stereo_position);
560                 REGISTER_CALLBACK (serv, X_("/master/select"), "f", master_select);
561                 REGISTER_CALLBACK (serv, X_("/monitor/gain"), "f", monitor_set_gain);
562                 REGISTER_CALLBACK (serv, X_("/monitor/fader"), "f", monitor_set_fader);
563                 REGISTER_CALLBACK (serv, X_("/monitor/db_delta"), "f", monitor_delta_gain);
564                 REGISTER_CALLBACK (serv, X_("/monitor/mute"), "i", monitor_set_mute);
565                 REGISTER_CALLBACK (serv, X_("/monitor/dim"), "i", monitor_set_dim);
566                 REGISTER_CALLBACK (serv, X_("/monitor/mono"), "i", monitor_set_mono);
567
568                 // Controls for the Selected strip
569                 REGISTER_CALLBACK (serv, X_("/select/recenable"), "i", sel_recenable);
570                 REGISTER_CALLBACK (serv, X_("/select/record_safe"), "i", sel_recsafe);
571                 REGISTER_CALLBACK (serv, X_("/select/mute"), "i", sel_mute);
572                 REGISTER_CALLBACK (serv, X_("/select/solo"), "i", sel_solo);
573                 REGISTER_CALLBACK (serv, X_("/select/solo_iso"), "i", sel_solo_iso);
574                 REGISTER_CALLBACK (serv, X_("/select/solo_safe"), "i", sel_solo_safe);
575                 REGISTER_CALLBACK (serv, X_("/select/monitor_input"), "i", sel_monitor_input);
576                 REGISTER_CALLBACK (serv, X_("/select/monitor_disk"), "i", sel_monitor_disk);
577                 REGISTER_CALLBACK (serv, X_("/select/polarity"), "i", sel_phase);
578                 REGISTER_CALLBACK (serv, X_("/select/gain"), "f", sel_gain);
579                 REGISTER_CALLBACK (serv, X_("/select/fader"), "f", sel_fader);
580                 REGISTER_CALLBACK (serv, X_("/select/db_delta"), "f", sel_dB_delta);
581                 REGISTER_CALLBACK (serv, X_("/select/trimdB"), "f", sel_trim);
582                 REGISTER_CALLBACK (serv, X_("/select/hide"), "i", sel_hide);
583                 REGISTER_CALLBACK (serv, X_("/select/pan_stereo_position"), "f", sel_pan_position);
584                 REGISTER_CALLBACK (serv, X_("/select/pan_stereo_width"), "f", sel_pan_width);
585                 REGISTER_CALLBACK (serv, X_("/select/send_gain"), "if", sel_sendgain);
586                 REGISTER_CALLBACK (serv, X_("/select/send_fader"), "if", sel_sendfader);
587                 REGISTER_CALLBACK (serv, X_("/select/send_enable"), "if", sel_sendenable);
588                 REGISTER_CALLBACK (serv, X_("/select/master_send_enable"), "i", sel_master_send_enable);
589                 REGISTER_CALLBACK (serv, X_("/select/send_page"), "f", sel_send_page);
590                 REGISTER_CALLBACK (serv, X_("/select/plug_page"), "f", sel_plug_page);
591                 REGISTER_CALLBACK (serv, X_("/select/plugin"), "f", sel_plugin);
592                 REGISTER_CALLBACK (serv, X_("/select/plugin/activate"), "f", sel_plugin_activate);
593                 REGISTER_CALLBACK (serv, X_("/select/expand"), "i", sel_expand);
594                 REGISTER_CALLBACK (serv, X_("/select/pan_elevation_position"), "f", sel_pan_elevation);
595                 REGISTER_CALLBACK (serv, X_("/select/pan_frontback_position"), "f", sel_pan_frontback);
596                 REGISTER_CALLBACK (serv, X_("/select/pan_lfe_control"), "f", sel_pan_lfe);
597                 REGISTER_CALLBACK (serv, X_("/select/comp_enable"), "f", sel_comp_enable);
598                 REGISTER_CALLBACK (serv, X_("/select/comp_threshold"), "f", sel_comp_threshold);
599                 REGISTER_CALLBACK (serv, X_("/select/comp_speed"), "f", sel_comp_speed);
600                 REGISTER_CALLBACK (serv, X_("/select/comp_mode"), "f", sel_comp_mode);
601                 REGISTER_CALLBACK (serv, X_("/select/comp_makeup"), "f", sel_comp_makeup);
602                 REGISTER_CALLBACK (serv, X_("/select/eq_enable"), "f", sel_eq_enable);
603                 REGISTER_CALLBACK (serv, X_("/select/eq_hpf/freq"), "f", sel_eq_hpf_freq);
604                 REGISTER_CALLBACK (serv, X_("/select/eq_hpf/enable"), "f", sel_eq_hpf_enable);
605                 REGISTER_CALLBACK (serv, X_("/select/eq_hpf/slope"), "f", sel_eq_hpf_slope);
606                 REGISTER_CALLBACK (serv, X_("/select/eq_lpf/freq"), "f", sel_eq_lpf_freq);
607                 REGISTER_CALLBACK (serv, X_("/select/eq_lpf/enable"), "f", sel_eq_lpf_enable);
608                 REGISTER_CALLBACK (serv, X_("/select/eq_lpf/slope"), "f", sel_eq_lpf_slope);
609                 REGISTER_CALLBACK (serv, X_("/select/eq_gain"), "if", sel_eq_gain);
610                 REGISTER_CALLBACK (serv, X_("/select/eq_freq"), "if", sel_eq_freq);
611                 REGISTER_CALLBACK (serv, X_("/select/eq_q"), "if", sel_eq_q);
612                 REGISTER_CALLBACK (serv, X_("/select/eq_shape"), "if", sel_eq_shape);
613
614                 /* These commands require the route index in addition to the arg; TouchOSC (et al) can't use these  */
615                 REGISTER_CALLBACK (serv, X_("/strip/mute"), "ii", route_mute);
616                 REGISTER_CALLBACK (serv, X_("/strip/solo"), "ii", route_solo);
617                 REGISTER_CALLBACK (serv, X_("/strip/solo_iso"), "ii", route_solo_iso);
618                 REGISTER_CALLBACK (serv, X_("/strip/solo_safe"), "ii", route_solo_safe);
619                 REGISTER_CALLBACK (serv, X_("/strip/recenable"), "ii", route_recenable);
620                 REGISTER_CALLBACK (serv, X_("/strip/record_safe"), "ii", route_recsafe);
621                 REGISTER_CALLBACK (serv, X_("/strip/monitor_input"), "ii", route_monitor_input);
622                 REGISTER_CALLBACK (serv, X_("/strip/monitor_disk"), "ii", route_monitor_disk);
623                 REGISTER_CALLBACK (serv, X_("/strip/expand"), "ii", strip_expand);
624                 REGISTER_CALLBACK (serv, X_("/strip/hide"), "ii", strip_hide);
625                 REGISTER_CALLBACK (serv, X_("/strip/select"), "ii", strip_gui_select);
626                 REGISTER_CALLBACK (serv, X_("/strip/polarity"), "ii", strip_phase);
627                 REGISTER_CALLBACK (serv, X_("/strip/gain"), "if", route_set_gain_dB);
628                 REGISTER_CALLBACK (serv, X_("/strip/fader"), "if", route_set_gain_fader);
629                 REGISTER_CALLBACK (serv, X_("/strip/trimdB"), "if", route_set_trim_dB);
630                 REGISTER_CALLBACK (serv, X_("/strip/pan_stereo_position"), "if", route_set_pan_stereo_position);
631                 REGISTER_CALLBACK (serv, X_("/strip/pan_stereo_width"), "if", route_set_pan_stereo_width);
632                 REGISTER_CALLBACK (serv, X_("/strip/plugin/parameter"), "iiif", route_plugin_parameter);
633                 // prints to cerr only
634                 REGISTER_CALLBACK (serv, X_("/strip/plugin/parameter/print"), "iii", route_plugin_parameter_print);
635                 REGISTER_CALLBACK (serv, X_("/strip/plugin/activate"), "ii", route_plugin_activate);
636                 REGISTER_CALLBACK (serv, X_("/strip/plugin/deactivate"), "ii", route_plugin_deactivate);
637                 REGISTER_CALLBACK (serv, X_("/strip/send/gain"), "iif", route_set_send_gain_dB);
638                 REGISTER_CALLBACK (serv, X_("/strip/send/fader"), "iif", route_set_send_fader);
639                 REGISTER_CALLBACK (serv, X_("/strip/send/enable"), "iif", route_set_send_enable);
640                 REGISTER_CALLBACK (serv, X_("/strip/name"), "is", route_rename);
641                 REGISTER_CALLBACK (serv, X_("/strip/sends"), "i", route_get_sends);
642                 REGISTER_CALLBACK (serv, X_("/strip/receives"), "i", route_get_receives);
643                 REGISTER_CALLBACK (serv, X_("/strip/plugin/list"), "i", route_plugin_list);
644                 REGISTER_CALLBACK (serv, X_("/strip/plugin/descriptor"), "ii", route_plugin_descriptor);
645                 REGISTER_CALLBACK (serv, X_("/strip/plugin/reset"), "ii", route_plugin_reset);
646
647                 /* still not-really-standardized query interface */
648                 //REGISTER_CALLBACK (serv, "/ardour/*/#current_value", "", current_value);
649                 //REGISTER_CALLBACK (serv, "/ardour/set", "", set);
650
651                 // un/register_update args= s:ctrl s:returl s:retpath
652                 //lo_server_add_method(serv, "/register_update", "sss", OSC::global_register_update_handler, this);
653                 //lo_server_add_method(serv, "/unregister_update", "sss", OSC::global_unregister_update_handler, this);
654                 //lo_server_add_method(serv, "/register_auto_update", "siss", OSC::global_register_auto_update_handler, this);
655                 //lo_server_add_method(serv, "/unregister_auto_update", "sss", OSC::_global_unregister_auto_update_handler, this);
656
657                 /* this is a special catchall handler,
658                  * register at the end so this is only called if no
659                  * other handler matches (also used for debug) */
660                 lo_server_add_method (serv, 0, 0, _catchall, this);
661         }
662 }
663
664 bool
665 OSC::osc_input_handler (IOCondition ioc, lo_server srv)
666 {
667         if (ioc & ~IO_IN) {
668                 return false;
669         }
670
671         if (ioc & IO_IN) {
672                 lo_server_recv (srv);
673         }
674
675         return true;
676 }
677
678 std::string
679 OSC::get_server_url()
680 {
681         string url;
682         char * urlstr;
683
684         if (_osc_server) {
685                 urlstr = lo_server_get_url (_osc_server);
686                 url = urlstr;
687                 free (urlstr);
688         }
689
690         return url;
691 }
692
693 std::string
694 OSC::get_unix_server_url()
695 {
696         string url;
697         char * urlstr;
698
699         if (_osc_unix_server) {
700                 urlstr = lo_server_get_url (_osc_unix_server);
701                 url = urlstr;
702                 free (urlstr);
703         }
704
705         return url;
706 }
707
708 void
709 OSC::gui_changed ()
710 {
711         session->set_dirty();
712 }
713
714 void
715 OSC::current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg)
716 {
717         char* subpath;
718
719         subpath = (char*) malloc (len-15+1);
720         memcpy (subpath, path, len-15);
721         subpath[len-15] = '\0';
722
723         send_current_value (subpath, argv, argc, msg);
724
725         free (subpath);
726 }
727
728 void
729 OSC::send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg)
730 {
731         if (!session) {
732                 return;
733         }
734
735         lo_message reply = lo_message_new ();
736         boost::shared_ptr<Route> r;
737         int id;
738
739         lo_message_add_string (reply, path);
740
741         if (argc == 0) {
742                 lo_message_add_string (reply, "bad syntax");
743         } else {
744                 id = argv[0]->i;
745                 r = session->get_remote_nth_route (id);
746
747                 if (!r) {
748                         lo_message_add_string (reply, "not found");
749                 } else {
750
751                         if (strcmp (path, X_("/strip/state")) == 0) {
752
753                                 if (boost::dynamic_pointer_cast<AudioTrack>(r)) {
754                                         lo_message_add_string (reply, "AT");
755                                 } else if (boost::dynamic_pointer_cast<MidiTrack>(r)) {
756                                         lo_message_add_string (reply, "MT");
757                                 } else {
758                                         lo_message_add_string (reply, "B");
759                                 }
760
761                                 lo_message_add_string (reply, r->name().c_str());
762                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
763                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
764                                 lo_message_add_int32 (reply, r->muted());
765                                 lo_message_add_int32 (reply, r->soloed());
766
767                         } else if (strcmp (path, X_("/strip/mute")) == 0) {
768
769                                 lo_message_add_int32 (reply, (float) r->muted());
770
771                         } else if (strcmp (path, X_("/strip/solo")) == 0) {
772
773                                 lo_message_add_int32 (reply, r->soloed());
774                         }
775                 }
776         }
777         OSCSurface *sur = get_surface(get_address (msg));
778
779         if (sur->feedback[14]) {
780                 lo_send_message (get_address (msg), X_("/reply"), reply);
781         } else {
782                 lo_send_message (get_address (msg), X_("#reply"), reply);
783         }
784         lo_message_free (reply);
785 }
786
787 int
788 OSC::_catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data)
789 {
790         return ((OSC*)user_data)->catchall (path, types, argv, argc, data);
791 }
792
793 int
794 OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
795 {
796         size_t len;
797         int ret = 1; /* unhandled */
798
799         //cerr << "Received a message, path = " << path << " types = \""
800         //     << (types ? types : "NULL") << '"' << endl;
801
802         /* 15 for /#current_value plus 2 for /<path> */
803
804         len = strlen (path);
805         OSCSurface *sur = get_surface(get_address (msg), true);
806         LinkSet *set;
807         uint32_t ls = sur->linkset;
808
809         if (ls) {
810                 set = &(link_sets[ls]);
811                 sur->custom_mode = set->custom_mode;
812                 sur->custom_strips = set->custom_strips;
813         }
814
815         if (strstr (path, X_("/automation"))) {
816                 ret = set_automation (path, types, argv, argc, msg);
817
818         } else
819         if (strstr (path, X_("/touch"))) {
820                 ret = touch_detect (path, types, argv, argc, msg);
821
822         } else
823         if (len >= 17 && !strcmp (&path[len-15], X_("/#current_value"))) {
824                 current_value_query (path, len, argv, argc, msg);
825                 ret = 0;
826
827         } else
828         if (!strncmp (path, X_("/cue/"), 5)) {
829
830                 ret = cue_parse (path, types, argv, argc, msg);
831
832         } else
833         if (!strncmp (path, X_("/select/plugin/parameter"), 24)) {
834
835                 ret = select_plugin_parameter (path, types, argv, argc, msg);
836
837         } else
838         if (!strncmp (path, X_("/access_action/"), 15)) {
839                 check_surface (msg);
840                 if (!(argc && !argv[0]->i)) {
841                         std::string action_path = path;
842
843                         access_action (action_path.substr(15));
844                 }
845
846                 ret = 0;
847         } else
848         if (strcmp (path, X_("/strip/listen")) == 0) {
849                 if (argc <= 0) {
850                         PBD::warning << "OSC: Wrong number of parameters." << endmsg;
851                 } else if (sur->custom_mode) {
852                         PBD::warning << "OSC: Can't add strips with custom enabled." << endmsg;
853                 } else {
854                         for (int n = 0; n < argc; ++n) {
855                                 boost::shared_ptr<Stripable> s = boost::shared_ptr<Stripable>();
856                                 if (types[n] == 'f') {
857                                         s = get_strip ((uint32_t) argv[n]->f, get_address (msg));
858                                 } else if (types[n] == 'i') {
859                                         s = get_strip (argv[n]->i, get_address (msg));
860                                 }
861                                 if (s) {
862                                         sur->custom_strips.push_back (s);
863                                 }
864                         }
865                         if (ls) {
866                                 set->custom_strips = sur->custom_strips;
867                         }
868                 }
869                 ret = 0;
870         } else
871         if (strcmp (path, X_("/strip/ignore")) == 0) {
872                 if (argc <= 0) {
873                         PBD::warning << "OSC: Wrong number of parameters." << endmsg;
874                 } else if (!sur->custom_mode) {
875                         PBD::warning << "OSC: Can't remove strips without custom enabled." << endmsg;
876                 } else {
877                         for (int n = 0; n < argc; ++n) {
878                                 uint32_t st_no = 0;
879                                 if (types[n] == 'f') {
880                                         st_no = (uint32_t) argv[n]->f;
881                                 } else if (types[n] == 'i') {
882                                         st_no = (uint32_t) argv[n]->i;
883                                 }
884                                 if (st_no && st_no <= sur->custom_strips.size ()) {
885                                         sur->custom_strips[argv[n]->i - 1] = boost::shared_ptr<Stripable>();
886                                 }
887                         }
888                         if (ls) {
889                                 set->custom_strips = sur->custom_strips;
890                         }
891                         ret = set_bank (sur->bank, msg);
892                 }
893
894                 ret = 0;
895         } else
896         if (strstr (path, X_("/strip")) && (argc != 1)) {
897                 // All of the strip commands below require 1 parameter
898                 PBD::warning << "OSC: Wrong number of parameters." << endmsg;
899         } else
900         if (!strncmp (path, X_("/strip/gain/"), 12) && strlen (path) > 12) {
901                 // in dB
902                 int ssid = atoi (&path[12]);
903                 ret = route_set_gain_dB (ssid, argv[0]->f, msg);
904         }
905         else if (!strncmp (path, X_("/strip/fader/"), 13) && strlen (path) > 13) {
906                 // in fader position
907                 int ssid = atoi (&path[13]);
908                 ret = route_set_gain_fader (ssid, argv[0]->f, msg);
909         }
910         else if (!strncmp (path, X_("/strip/db_delta"), 15)) {
911                 // in db delta
912                 int ssid;
913                 int ar_off = 0;
914                 float delta;
915                 if (strlen (path) > 15 && argc == 1) {
916                         ssid = atoi (&path[16]);
917                 } else if (argc == 2) {
918                         if (types[0] == 'f') {
919                                 ssid = (int) argv[0]->f;
920                         } else {
921                                 ssid = argv[0]->i;
922                         }
923                         ar_off = 1;
924                 } else {
925                         return -1;
926                 }
927                 if (types[ar_off] == 'f') {
928                         delta = argv[ar_off]->f;
929                 } else {
930                         delta = (float) argv[ar_off]->i;
931                 }
932                 ret = strip_db_delta (ssid, delta, msg);
933         }
934         else if (!strncmp (path, X_("/strip/trimdB/"), 14) && strlen (path) > 14) {
935                 int ssid = atoi (&path[14]);
936                 ret = route_set_trim_dB (ssid, argv[0]->f, msg);
937         }
938         else if (!strncmp (path, X_("/strip/pan_stereo_position/"), 27) && strlen (path) > 27) {
939                 int ssid = atoi (&path[27]);
940                 ret = route_set_pan_stereo_position (ssid, argv[0]->f, msg);
941         }
942         else if (!strncmp (path, X_("/strip/mute/"), 12) && strlen (path) > 12) {
943                 int ssid = atoi (&path[12]);
944                 ret = route_mute (ssid, argv[0]->i, msg);
945         }
946         else if (!strncmp (path, X_("/strip/solo/"), 12) && strlen (path) > 12) {
947                 int ssid = atoi (&path[12]);
948                 ret = route_solo (ssid, argv[0]->i, msg);
949         }
950         else if (!strncmp (path, X_("/strip/monitor_input/"), 21) && strlen (path) > 21) {
951                 int ssid = atoi (&path[21]);
952                 ret = route_monitor_input (ssid, argv[0]->i, msg);
953         }
954         else if (!strncmp (path, X_("/strip/monitor_disk/"), 20) && strlen (path) > 20) {
955                 int ssid = atoi (&path[20]);
956                 ret = route_monitor_disk (ssid, argv[0]->i, msg);
957         }
958         else if (!strncmp (path, X_("/strip/recenable/"), 17) && strlen (path) > 17) {
959                 int ssid = atoi (&path[17]);
960                 ret = route_recenable (ssid, argv[0]->i, msg);
961         }
962         else if (!strncmp (path, X_("/strip/record_safe/"), 19) && strlen (path) > 19) {
963                 int ssid = atoi (&path[19]);
964                 ret = route_recsafe (ssid, argv[0]->i, msg);
965         }
966         else if (!strncmp (path, X_("/strip/expand/"), 14) && strlen (path) > 14) {
967                 int ssid = atoi (&path[14]);
968                 ret = strip_expand (ssid, argv[0]->i, msg);
969         }
970         else if (!strncmp (path, X_("/strip/hide/"), 12) && strlen (path) > 12) {
971                 int ssid = atoi (&path[12]);
972                 ret = strip_hide (ssid, argv[0]->i, msg);
973         }
974         else if (!strncmp (path, X_("/strip/select/"), 14) && strlen (path) > 14) {
975                 int ssid = atoi (&path[14]);
976                 ret = strip_gui_select (ssid, argv[0]->i, msg);
977         } else
978         if (strstr (path, X_("/select")) && (argc != 1)) {
979                 // All of the select commands below require 1 parameter
980                 PBD::warning << "OSC: Wrong number of parameters." << endmsg;
981         }
982         else if (!strncmp (path, X_("/select/send_gain/"), 18) && strlen (path) > 18) {
983                 int ssid = atoi (&path[18]);
984                 ret = sel_sendgain (ssid, argv[0]->f, msg);
985         }
986         else if (!strncmp (path, X_("/select/send_fader/"), 19) && strlen (path) > 19) {
987                 int ssid = atoi (&path[19]);
988                 ret = sel_sendfader (ssid, argv[0]->f, msg);
989         }
990         else if (!strncmp (path, X_("/select/send_enable/"), 20) && strlen (path) > 20) {
991                 int ssid = atoi (&path[20]);
992                 ret = sel_sendenable (ssid, argv[0]->f, msg);
993         }
994         else if (!strncmp (path, X_("/select/eq_gain/"), 16) && strlen (path) > 16) {
995                 int ssid = atoi (&path[16]);
996                 ret = sel_eq_gain (ssid, argv[0]->f, msg);
997         }
998         else if (!strncmp (path, X_("/select/eq_freq/"), 16) && strlen (path) > 16) {
999                 int ssid = atoi (&path[16]);
1000                 ret = sel_eq_freq (ssid, argv[0]->f , msg);
1001         }
1002         else if (!strncmp (path, X_("/select/eq_q/"), 13) && strlen (path) > 13) {
1003                 int ssid = atoi (&path[13]);
1004                 ret = sel_eq_q (ssid, argv[0]->f, msg);
1005         }
1006         else if (!strncmp (path, X_("/select/eq_shape/"), 17) && strlen (path) > 17) {
1007                 int ssid = atoi (&path[17]);
1008                 ret = sel_eq_shape (ssid, argv[0]->f, msg);
1009         }
1010         else if (!strncmp (path, X_("/marker"), 7)) {
1011                 ret = set_marker (types, argv, argc, msg);
1012         }
1013         else if (!strncmp (path, X_("/set_surface"), 12)) {
1014                 ret = surface_parse (path, types, argv, argc, msg);
1015         }
1016         else if (strstr (path, X_("/link"))) {
1017                 ret = parse_link (path, types, argv, argc, msg);
1018         }
1019         if (ret) {
1020                 check_surface (msg);
1021         }
1022
1023         if ((ret && _debugmode != Off)) {
1024                 debugmsg (_("Unhandled OSC message"), path, types, argv, argc);
1025         } else if (!ret && _debugmode == All) {
1026                 debugmsg (_("OSC"), path, types, argv, argc);
1027         }
1028
1029         return ret;
1030 }
1031
1032 void
1033 OSC::debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc)
1034 {
1035         std::stringstream ss;
1036         for (int i = 0; i < argc; ++i) {
1037                 lo_type type = (lo_type)types[i];
1038                         ss << " ";
1039                 switch (type) {
1040                         case LO_INT32:
1041                                 ss << "i:" << argv[i]->i;
1042                                 break;
1043                         case LO_FLOAT:
1044                                 ss << "f:" << argv[i]->f;
1045                                 break;
1046                         case LO_DOUBLE:
1047                                 ss << "d:" << argv[i]->d;
1048                                 break;
1049                         case LO_STRING:
1050                                 ss << "s:" << &argv[i]->s;
1051                                 break;
1052                         case LO_INT64:
1053                                 ss << "h:" << argv[i]->h;
1054                                 break;
1055                         case LO_CHAR:
1056                                 ss << "c:" << argv[i]->s;
1057                                 break;
1058                         case LO_TIMETAG:
1059                                 ss << "<Timetag>";
1060                                 break;
1061                         case LO_BLOB:
1062                                 ss << "<BLOB>";
1063                                 break;
1064                         case LO_TRUE:
1065                                 ss << "#T";
1066                                 break;
1067                         case LO_FALSE:
1068                                 ss << "#F";
1069                                 break;
1070                         case LO_NIL:
1071                                 ss << "NIL";
1072                                 break;
1073                         case LO_INFINITUM:
1074                                 ss << "#inf";
1075                                 break;
1076                         case LO_MIDI:
1077                                 ss << "<MIDI>";
1078                                 break;
1079                         case LO_SYMBOL:
1080                                 ss << "<SYMBOL>";
1081                                 break;
1082                         default:
1083                                 ss << "< ?? >";
1084                                 break;
1085                 }
1086         }
1087         PBD::info << prefix << ": " << path << ss.str() << endmsg;
1088 }
1089
1090 // "Application Hook" Handlers //
1091 void
1092 OSC::session_loaded (Session& s)
1093 {
1094 //      lo_address listener = lo_address_new (NULL, "7770");
1095 //      lo_send (listener, "/session/loaded", "ss", s.path().c_str(), s.name().c_str());
1096 }
1097
1098 void
1099 OSC::session_exported (std::string path, std::string name)
1100 {
1101         lo_address listener = lo_address_new (NULL, "7770");
1102         lo_send (listener, X_("/session/exported"), "ss", path.c_str(), name.c_str());
1103         lo_address_free (listener);
1104 }
1105
1106 // end "Application Hook" Handlers //
1107
1108 /* path callbacks */
1109
1110 int
1111 OSC::current_value (const char */*path*/, const char */*types*/, lo_arg **/*argv*/, int /*argc*/, void */*data*/, void* /*user_data*/)
1112 {
1113 #if 0
1114         const char* returl;
1115
1116         if (argc < 3 || types == 0 || strlen (types) < 3 || types[0] != 's' || types[1] != 's' || types[2] != s) {
1117                 return 1;
1118         }
1119
1120         const char *returl = argv[1]->s;
1121         lo_address addr = find_or_cache_addr (returl);
1122
1123         const char *retpath = argv[2]->s;
1124
1125
1126         if (strcmp (argv[0]->s, X_("transport_frame")) == 0) {
1127
1128                 if (session) {
1129                         lo_send (addr, retpath, "i", session->transport_sample());
1130                 }
1131
1132         } else if (strcmp (argv[0]->s, X_("transport_speed")) == 0) {
1133
1134                 if (session) {
1135                         lo_send (addr, retpath, "i", session->transport_sample());
1136                 }
1137
1138         } else if (strcmp (argv[0]->s, X_("transport_locked")) == 0) {
1139
1140                 if (session) {
1141                         lo_send (addr, retpath, "i", session->transport_sample());
1142                 }
1143
1144         } else if (strcmp (argv[0]->s, X_("punch_in")) == 0) {
1145
1146                 if (session) {
1147                         lo_send (addr, retpath, "i", session->transport_sample());
1148                 }
1149
1150         } else if (strcmp (argv[0]->s, X_("punch_out")) == 0) {
1151
1152                 if (session) {
1153                         lo_send (addr, retpath, "i", session->transport_sample());
1154                 }
1155
1156         } else if (strcmp (argv[0]->s, X_("rec_enable")) == 0) {
1157
1158                 if (session) {
1159                         lo_send (addr, retpath, "i", session->transport_sample());
1160                 }
1161
1162         } else {
1163
1164                 /* error */
1165         }
1166 #endif
1167         return 0;
1168 }
1169
1170 void
1171 OSC::routes_list (lo_message msg)
1172 {
1173         if (!session) {
1174                 return;
1175         }
1176         OSCSurface *sur = get_surface(get_address (msg), true);
1177
1178         for (int n = 0; n < (int) sur->nstrips; ++n) {
1179
1180                 boost::shared_ptr<Stripable> s = get_strip (n + 1, get_address (msg));
1181
1182                 if (s) {
1183                         // some things need the route
1184                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
1185
1186                         lo_message reply = lo_message_new ();
1187
1188                         if (boost::dynamic_pointer_cast<AudioTrack>(s)) {
1189                                 lo_message_add_string (reply, "AT");
1190                         } else if (boost::dynamic_pointer_cast<MidiTrack>(s)) {
1191                                 lo_message_add_string (reply, "MT");
1192                         } else if (boost::dynamic_pointer_cast<VCA>(s)) {
1193                                 lo_message_add_string (reply, "V");
1194                         } else if (s->is_master()) {
1195                                 lo_message_add_string (reply, "MA");
1196                         } else if (s->is_monitor()) {
1197                                 lo_message_add_string (reply, "MO");
1198                         } else if (boost::dynamic_pointer_cast<Route>(s) && !boost::dynamic_pointer_cast<Track>(s)) {
1199                                 if (!(s->presentation_info().flags() & PresentationInfo::MidiBus)) {
1200                                         // r->feeds (session->master_out()) may make more sense
1201                                         if (r->direct_feeds_according_to_reality (session->master_out())) {
1202                                                 // this is a bus
1203                                                 lo_message_add_string (reply, "B");
1204                                         } else {
1205                                                 // this is an Aux out
1206                                                 lo_message_add_string (reply, "AX");
1207                                         }
1208                                 } else {
1209                                         lo_message_add_string (reply, "MB");
1210                                 }
1211                         }
1212
1213                         lo_message_add_string (reply, s->name().c_str());
1214                         if (r) {
1215                                 // routes have inputs and outputs
1216                                 lo_message_add_int32 (reply, r->n_inputs().n_audio());
1217                                 lo_message_add_int32 (reply, r->n_outputs().n_audio());
1218                         } else {
1219                                 // non-routes like VCAs don't
1220                                 lo_message_add_int32 (reply, 0);
1221                                 lo_message_add_int32 (reply, 0);
1222                         }
1223                         if (s->mute_control()) {
1224                                 lo_message_add_int32 (reply, s->mute_control()->get_value());
1225                         } else {
1226                                 lo_message_add_int32 (reply, 0);
1227                         }
1228                         if (s->solo_control()) {
1229                                 lo_message_add_int32 (reply, s->solo_control()->get_value());
1230                         } else {
1231                                 lo_message_add_int32 (reply, 0);
1232                         }
1233                         lo_message_add_int32 (reply, n + 1);
1234                         if (s->rec_enable_control()) {
1235                                 lo_message_add_int32 (reply, s->rec_enable_control()->get_value());
1236                         }
1237                         if (sur->feedback[14]) {
1238                                 lo_send_message (get_address (msg), X_("/reply"), reply);
1239                         } else {
1240                                 lo_send_message (get_address (msg), X_("#reply"), reply);
1241                         }
1242                         lo_message_free (reply);
1243                 }
1244         }
1245
1246         // Send end of listing message
1247         lo_message reply = lo_message_new ();
1248
1249         lo_message_add_string (reply, X_("end_route_list"));
1250         lo_message_add_int64 (reply, session->sample_rate());
1251         lo_message_add_int64 (reply, session->current_end_sample());
1252         if (session->monitor_out()) {
1253                 // this session has a monitor section
1254                 lo_message_add_int32 (reply, 1);
1255         } else {
1256                 lo_message_add_int32 (reply, 0);
1257         }
1258
1259         if (sur->feedback[14]) {
1260                 lo_send_message (get_address (msg), X_("/reply"), reply);
1261         } else {
1262                 lo_send_message (get_address (msg), X_("#reply"), reply);
1263         }
1264
1265         lo_message_free (reply);
1266         // send feedback for newly created control surface
1267         strip_feedback (sur, true);
1268         global_feedback (sur);
1269         _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), get_address (msg));
1270
1271 }
1272
1273 void
1274 OSC::surface_list (lo_message msg)
1275 {
1276                 get_surfaces ();
1277 }
1278
1279 void
1280 OSC::get_surfaces ()
1281 {
1282
1283         /* this function is for debugging and prints lots of
1284          * information about what surfaces Ardour knows about and their
1285          * internal parameters. It is best accessed by sending:
1286          * /surface/list from oscsend. This command does not create
1287          * a surface entry.
1288          */
1289
1290         PBD::info << string_compose ("\nList of known Surfaces (%1):\n", _surface.size());
1291
1292         Glib::Threads::Mutex::Lock lm (surfaces_lock);
1293         for (uint32_t it = 0; it < _surface.size(); it++) {
1294                 OSCSurface* sur = &_surface[it];
1295                 char *chost = lo_url_get_hostname (sur->remote_url.c_str());
1296                 string host = chost;
1297                 free (chost);
1298                 string port = get_port (host);
1299                 if (port != "auto") {
1300                         port = "Manual port";
1301                 } else {
1302                         port = "Auto port";
1303                 }
1304                 PBD::info << string_compose ("\n  Surface: %1 - URL: %2  %3\n", it, sur->remote_url, port);
1305                 PBD::info << string_compose ("  Number of strips: %1   Bank size: %2   Current Bank %3\n", sur->nstrips, sur->bank_size, sur->bank);
1306                 PBD::info << string_compose ("  Use Custom: %1   Custom Strips: %2\n", sur->custom_mode, sur->custom_strips.size ());
1307                 bool ug = false;
1308                 if (sur->usegroup == PBD::Controllable::UseGroup) {
1309                         ug = true;
1310                 }
1311                 PBD::info << string_compose ("  Strip Types: %1   Feedback: %2   No_clear flag: %3   Gain mode: %4   Use groups flag %5\n", \
1312                         sur->strip_types.to_ulong(), sur->feedback.to_ulong(), sur->no_clear, sur->gainmode, ug);
1313                 PBD::info << string_compose ("  Using plugin: %1  of  %2 plugins, with %3 params.  Page size: %4  Page: %5\n", \
1314                         sur->plugin_id, sur->plugins.size(), sur->plug_params.size(), sur->plug_page_size, sur->plug_page);
1315                 PBD::info << string_compose ("  Send page size: %1  Page: %2\n", sur->send_page_size, sur->send_page);
1316                 PBD::info << string_compose ("  Expanded flag %1   Track: %2   Jogmode: %3\n", sur->expand_enable, sur->expand, sur->jogmode);
1317                 PBD::info << string_compose ("  Personal monitor flag %1,   Aux master: %2,   Number of sends: %3\n", sur->cue, sur->aux, sur->sends.size());
1318                 PBD::info << string_compose ("  Linkset: %1   Device Id: %2\n", sur->linkset, sur->linkid);
1319         }
1320         PBD::info << string_compose ("\nList of LinkSets (%1):\n", link_sets.size());
1321         std::map<uint32_t, LinkSet>::iterator it;
1322         for (it = link_sets.begin(); it != link_sets.end(); it++) {
1323                 if (!(*it).first) {
1324                         continue;
1325                 }
1326                 uint32_t devices = 0;
1327                 LinkSet* set = &(*it).second;
1328                 if (set->urls.size()) {
1329                         devices = set->urls.size() - 1;
1330                 }
1331                 PBD::info << string_compose ("\n  Linkset %1 has %2 devices and sees %3 strips\n", (*it).first, devices, set->strips.size());
1332                 PBD::info << string_compose ("  Bank size: %1   Current bank: %2   Strip Types: %3\n", set->banksize, set->bank, set->strip_types.to_ulong());
1333                 PBD::info << string_compose ("  Auto bank sizing: %1 Linkset not ready flag: %2\n", set->autobank, set->not_ready);
1334                 PBD::info << string_compose ("  Use Custom: %1 Number of Custom Strips: %2\n", set->custom_mode, set->custom_strips.size ());
1335         }
1336         PBD::info << endmsg;
1337 }
1338
1339 int
1340 OSC::custom_clear (lo_message msg)
1341 {
1342         if (!session) {
1343                 return 0;
1344         }
1345         OSCSurface *sur = get_surface(get_address (msg), true);
1346         sur->custom_mode = 0;
1347         sur->custom_strips.clear ();
1348         sur->strips = get_sorted_stripables(sur->strip_types, sur->cue, false, sur->custom_strips);
1349         sur->nstrips = sur->strips.size();
1350         LinkSet *set;
1351         uint32_t ls = sur->linkset;
1352         if (ls) {
1353                 set = &(link_sets[ls]);
1354                 set->custom_mode = 0;
1355                 set->custom_strips.clear ();
1356                 set->strips = sur->strips;
1357         }
1358         return set_bank (1, msg);
1359 }
1360
1361 int
1362 OSC::custom_mode (float state, lo_message msg)
1363 {
1364         if (!session) {
1365                 return 0;
1366         }
1367         OSCSurface *sur = get_surface(get_address (msg), true);
1368         LinkSet *set;
1369         uint32_t ls = sur->linkset;
1370
1371         if (ls) {
1372                 set = &(link_sets[ls]);
1373                 sur->custom_mode = set->custom_mode;
1374                 sur->custom_strips = set->custom_strips;
1375         }
1376         if (state > 0){
1377                 if (sur->custom_strips.size () == 0) {
1378                         PBD::warning << "No custom strips set to enable" << endmsg;
1379                         sur->custom_mode = 0;
1380                         if (ls) {
1381                                 set->custom_mode = 0;
1382                         }
1383                         return -1;
1384                 } else {
1385                         if (sur->bank_size) {
1386                                 sur->custom_mode = (uint32_t) state | 0x4;
1387                         } else {
1388                                 sur->custom_mode = (uint32_t) state;
1389                         }
1390                         sur->strips = get_sorted_stripables(sur->strip_types, sur->cue, sur->custom_mode, sur->custom_strips);
1391                         sur->nstrips = sur->custom_strips.size();
1392                 }
1393         } else {
1394                 sur->custom_mode = 0;
1395                 sur->strips = get_sorted_stripables(sur->strip_types, sur->cue, 0, sur->custom_strips);
1396                 sur->nstrips = sur->strips.size();
1397         }
1398         if (ls) {
1399                 set->custom_mode = sur->custom_mode;
1400                 set->strips = sur->strips;
1401         }
1402         return set_bank (1, msg);
1403 }
1404
1405 int
1406 OSC::cancel_all_solos ()
1407 {
1408         session->cancel_all_solo ();
1409         return 0;
1410 }
1411
1412 lo_address
1413 OSC::get_address (lo_message msg)
1414 {
1415         lo_address addr = lo_message_get_source (msg);
1416         string host = lo_address_get_hostname (addr);
1417         string port = lo_address_get_port (addr);
1418         int protocol = lo_address_get_protocol (addr);
1419         string saved_port = get_port (host);
1420         if (saved_port != "") {
1421                 if (saved_port != "auto") {
1422                         port = saved_port;
1423                         return lo_address_new_with_proto (protocol, host.c_str(), port.c_str());
1424                 } else {
1425                         return lo_message_get_source (msg);
1426                 }
1427         }
1428
1429         // if we get here we need to add a new entry for this surface
1430         PortAdd new_port;
1431         new_port.host = host;
1432         if (address_only) {
1433                 new_port.port = remote_port;
1434                 _ports.push_back (new_port);
1435                 return lo_address_new_with_proto (protocol, host.c_str(), remote_port.c_str());
1436         } else {
1437                 new_port.port = "auto";
1438                 _ports.push_back (new_port);
1439                 return lo_message_get_source (msg);
1440         }
1441 }
1442
1443 string
1444 OSC::get_port (string host)
1445 {
1446         for (uint32_t i = 0; i < _ports.size (); i++) {
1447                 if (_ports[i].host == host) {
1448                         return _ports[i].port;
1449                 }
1450         }
1451         return "";
1452 }
1453
1454 int
1455 OSC::refresh_surface (lo_message msg)
1456 {
1457         OSCSurface *s = get_surface(get_address (msg), true);
1458         uint32_t bs = s->bank_size;
1459         uint32_t st = (uint32_t) s->strip_types.to_ulong();
1460         uint32_t fb = (uint32_t) s->feedback.to_ulong();
1461         uint32_t gm = (uint32_t) s->gainmode;
1462         uint32_t sp = s->send_page_size;
1463         uint32_t pp = s->plug_page_size;
1464
1465         surface_destroy (s);
1466         // restart all observers
1467         set_surface (bs, st, fb, gm, sp, pp, msg);
1468         return 0;
1469 }
1470
1471 void
1472 OSC::clear_devices ()
1473 {
1474         tick = false;
1475         observer_busy = true;
1476         session_connections.drop_connections ();
1477         // clear out surfaces
1478         for (uint32_t it = 0; it < _surface.size(); ++it) {
1479                 OSCSurface* sur = &_surface[it];
1480                 surface_destroy (sur);
1481         }
1482         _surface.clear();
1483         link_sets.clear ();
1484         _ports.clear ();
1485
1486         PresentationInfo::Change.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
1487
1488         observer_busy = false;
1489         tick = true;
1490 }
1491
1492 int
1493 OSC::parse_link (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
1494 {
1495         int ret = 1; /* unhandled */
1496         int set = 0;
1497         if (!argc) {
1498                 PBD::warning << "OSC: /link/* needs at least one parameter" << endmsg;
1499                 return ret;
1500         }
1501         float data = 0;
1502         if (types[argc - 1] == 'f') {
1503                 data = argv[argc - 1]->f;
1504         } else {
1505                 data = argv[argc - 1]->i;
1506         }
1507         if (isdigit(strrchr (path, '/')[1])) {
1508                 set = atoi (&(strrchr (path, '/')[1]));
1509         } else if (argc == 2) {
1510                 if (types[0] == 'f') {
1511                         set = (int) argv[0]->f;
1512                 } else {
1513                         set = argv[0]->i;
1514                 }
1515         } else {
1516                 PBD::warning << "OSC: wrong number of parameters." << endmsg;
1517                 return ret;
1518         }
1519         LinkSet *ls = get_linkset (set, get_address (msg));
1520
1521         if (!set) {
1522                 return 0;
1523         }
1524         if (!strncmp (path, X_("/link/bank_size"), 15)) {
1525                 ls->banksize = (uint32_t) data;
1526                 ls->autobank = false;
1527                 ls->not_ready = link_check (set);
1528                 if (ls->not_ready) {
1529                         ls->bank = 1;
1530                         surface_link_state (ls);
1531                 } else {
1532                         _set_bank (ls->bank, get_address (msg));
1533                 }
1534                 ret = 0;
1535
1536         } else if (!strncmp (path, X_("/link/set"), 9)) {
1537                 ret = set_link (set, (uint32_t) data, get_address (msg));
1538         }
1539
1540         return ret;
1541 }
1542
1543 OSC::LinkSet *
1544 OSC::get_linkset (uint32_t set, lo_address addr)
1545 {
1546         OSCSurface *sur = get_surface(addr);
1547         LinkSet *ls = 0;
1548
1549         if (set) {
1550                 // need to check if set is wanted
1551                 std::map<uint32_t, LinkSet>::iterator it;
1552                 it = link_sets.find(set);
1553                 if (it == link_sets.end()) {
1554                         // no such linkset make it
1555                         LinkSet new_ls;
1556                         new_ls.banksize = 0;
1557                         new_ls.bank = 1;
1558                         new_ls.autobank = true;
1559                         new_ls.not_ready = true;
1560                         new_ls.strip_types = sur->strip_types;
1561                         new_ls.strips = sur->strips;
1562                         new_ls.custom_strips = sur->custom_strips;
1563                         new_ls.custom_mode = sur->custom_mode;
1564                         new_ls.urls.resize (2);
1565                         link_sets[set] = new_ls;
1566                 }
1567                 ls = &link_sets[set];
1568
1569         } else {
1570                 // User expects this surface to be removed from any sets
1571                 uint32_t oldset = sur->linkset;
1572                 if (oldset) {
1573                         uint32_t oldid = sur->linkid;
1574                         sur->linkid = 1;
1575                         sur->linkset = 0;
1576                         LinkSet *ols = &link_sets[oldset];
1577                         if (ols) {
1578                                 ols->not_ready = oldid;
1579                                 ols->urls[oldid] = "";
1580                                 surface_link_state (ols);
1581                         }
1582                 }
1583         }
1584         return ls;
1585 }
1586
1587 int
1588 OSC::set_link (uint32_t set, uint32_t id, lo_address addr)
1589 {
1590         OSCSurface *sur = get_surface(addr, true);
1591         sur->linkset = set;
1592         sur->linkid = id;
1593         LinkSet *ls = get_linkset (set, addr);
1594         if (ls->urls.size() <= (uint32_t) id) {
1595                 ls->urls.resize ((int) id + 1);
1596         }
1597         ls->urls[(uint32_t) id] = sur->remote_url;
1598         ls->not_ready = link_check (set);
1599         if (ls->not_ready) {
1600                 surface_link_state (ls);
1601         } else {
1602                 _set_bank (1, addr);
1603         }
1604         return 0;
1605 }
1606
1607 void
1608 OSC::link_strip_types (uint32_t linkset, uint32_t striptypes)
1609 {
1610         LinkSet *ls = 0;
1611
1612         if (!linkset) {
1613                 return;
1614         }
1615         std::map<uint32_t, LinkSet>::iterator it;
1616         it = link_sets.find(linkset);
1617         if (it == link_sets.end()) {
1618                 // this should never happen... but
1619                 return;
1620         }
1621         ls = &link_sets[linkset];
1622         ls->strip_types = striptypes;
1623         for (uint32_t dv = 1; dv < ls->urls.size(); dv++) {
1624                 OSCSurface *su;
1625
1626                 if (ls->urls[dv] != "") {
1627                         string url = ls->urls[dv];
1628                         su = get_surface (lo_address_new_from_url (url.c_str()), true);
1629                         if (su->linkset == linkset) {
1630                                 su->strip_types = striptypes;
1631                                 if (su->strip_types[10]) {
1632                                         su->usegroup = PBD::Controllable::UseGroup;
1633                                 } else {
1634                                         su->usegroup = PBD::Controllable::NoGroup;
1635                                 }
1636                         } else {
1637                                 ls->urls[dv] = "";
1638                         }
1639                 }
1640         }
1641 }
1642
1643 void
1644 OSC::surface_link_state (LinkSet * set)
1645 {
1646         for (uint32_t dv = 1; dv < set->urls.size(); dv++) {
1647
1648                 if (set->urls[dv] != "") {
1649                         string url = set->urls[dv];
1650                         OSCSurface *sur = get_surface (lo_address_new_from_url (url.c_str()), true);
1651                         for (uint32_t i = 0; i < sur->observers.size(); i++) {
1652                                 sur->observers[i]->set_link_ready (set->not_ready);
1653                         }
1654                 }
1655         }
1656 }
1657
1658 int
1659 OSC::link_check (uint32_t set)
1660 {
1661         LinkSet *ls = 0;
1662
1663         if (!set) {
1664                 return 1;
1665         }
1666         std::map<uint32_t, LinkSet>::iterator it;
1667         it = link_sets.find(set);
1668         if (it == link_sets.end()) {
1669                 // this should never happen... but
1670                 return 1;
1671         }
1672         ls = &link_sets[set];
1673         uint32_t bank_total = 0;
1674         for (uint32_t dv = 1; dv < ls->urls.size(); dv++) {
1675                 OSCSurface *su;
1676
1677                 if (ls->urls[dv] != "") {
1678                         string url = ls->urls[dv];
1679                         su = get_surface (lo_address_new_from_url (url.c_str()), true);
1680                 } else {
1681                         return dv;
1682                 }
1683                 if (su->linkset == set) {
1684                         bank_total = bank_total + su->bank_size;
1685                 } else {
1686                         ls->urls[dv] = "";
1687                         return dv;
1688                 }
1689                 if (ls->autobank) {
1690                         ls->banksize = bank_total;
1691                 } else {
1692                         if (bank_total != ls->banksize) {
1693                                 return ls->urls.size();
1694                         }
1695                 }
1696         }
1697         return 0;
1698 }
1699
1700 int
1701 OSC::surface_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
1702 {
1703         int ret = 1; /* unhandled */
1704         OSCSurface *sur = get_surface(get_address (msg), true);
1705         int pi_page = sur->plug_page_size;
1706         int se_page = sur->send_page_size;
1707         int fadermode = sur->gainmode;
1708         int feedback = sur->feedback.to_ulong();
1709         int strip_types = sur->strip_types.to_ulong();
1710         int bank_size = sur->bank_size;
1711         int linkset = sur->linkset;
1712         int linkid = sur->linkid;
1713         string host = lo_url_get_hostname(sur->remote_url.c_str());
1714         int port = atoi (get_port (host).c_str());
1715
1716         if (argc == 1 && !strncmp (path, X_("/set_surface/feedback"), 21)) {
1717                 if (types[0] == 'f') {
1718                         ret = set_surface_feedback ((int)argv[0]->f, msg);
1719                 } else {
1720                         ret = set_surface_feedback (argv[0]->i, msg);
1721                 }
1722         }
1723         else if (argc == 1 && !strncmp (path, X_("/set_surface/bank_size"), 22)) {
1724                 if (types[0] == 'f') {
1725                         ret = set_surface_bank_size ((int)argv[0]->f, msg);
1726                 } else {
1727                         ret = set_surface_bank_size (argv[0]->i, msg);
1728                 }
1729         }
1730         else if (argc == 1 && !strncmp (path, X_("/set_surface/gainmode"), 21)) {
1731                 if (types[0] == 'f') {
1732                         ret = set_surface_gainmode ((int)argv[0]->f, msg);
1733                 } else {
1734                         ret = set_surface_gainmode (argv[0]->i, msg);
1735                 }
1736         }
1737         else if (argc == 1 && !strncmp (path, X_("/set_surface/strip_types"), 24)) {
1738                 if (types[0] == 'f') {
1739                         ret = set_surface_strip_types ((int)argv[0]->f, msg);
1740                 } else {
1741                         ret = set_surface_strip_types (argv[0]->i, msg);
1742                 }
1743         }
1744         else if (argc == 1 && !strncmp (path, X_("/set_surface/send_page_size"), 27)) {
1745                 if (types[0] == 'f') {
1746                         ret = sel_send_pagesize ((int)argv[0]->f, msg);
1747                 } else {
1748                         ret = sel_send_pagesize (argv[0]->i, msg);
1749                 }
1750         }
1751         else if (argc == 1 && !strncmp (path, X_("/set_surface/plugin_page_size"), 29)) {
1752                 if (types[0] == 'f') {
1753                         ret = sel_plug_pagesize ((int)argv[0]->f, msg);
1754                 } else {
1755                         ret = sel_plug_pagesize (argv[0]->i, msg);
1756                 }
1757         }
1758         else if (argc == 1 && !strncmp (path, X_("/set_surface/port"), 17)) {
1759                 if (types[0] == 'f') {
1760                         ret = set_surface_port ((int)argv[0]->f, msg);
1761                 } else {
1762                         ret = set_surface_port (argv[0]->i, msg);
1763                 }
1764         } else if (strlen(path) == 12) {
1765
1766                 // command is in /set_surface iii form
1767                 switch (argc) {
1768                         case 9:
1769                                 if (types[8] == 'f') {
1770                                         linkid = (int) argv[8]->f;
1771                                 } else {
1772                                         linkid = argv[8]->i;
1773                                 }
1774                         case 8:
1775                                 if (types[7] == 'f') {
1776                                         linkset = (int) argv[7]->f;
1777                                 } else {
1778                                         linkset = argv[7]->i;
1779                                 }
1780                         case 7:
1781                                 if (types[6] == 'f') {
1782                                         port = (int) argv[6]->f;
1783                                 } else {
1784                                         port = argv[6]->i;
1785                                 }
1786                         case 6:
1787                                 if (types[5] == 'f') {
1788                                         pi_page = (int) argv[5]->f;
1789                                 } else {
1790                                         pi_page = argv[5]->i;
1791                                 }
1792                         case 5:
1793                                 if (types[4] == 'f') {
1794                                         se_page = (int) argv[4]->f;
1795                                 } else {
1796                                         se_page = argv[4]->i;
1797                                 }
1798                         case 4:
1799                                 if (types[3] == 'f') {
1800                                         fadermode = (int) argv[3]->f;
1801                                 } else {
1802                                         fadermode = argv[3]->i;
1803                                 }
1804                         case 3:
1805                                 if (types[2] == 'f') {
1806                                         feedback = (int) argv[2]->f;
1807                                 } else {
1808                                         feedback = argv[2]->i;
1809                                 }
1810                         case 2:
1811                                 if (types[1] == 'f') {
1812                                         strip_types = (int) argv[1]->f;
1813                                 } else {
1814                                         strip_types = argv[1]->i;
1815                                 }
1816                         case 1:
1817                                 if (types[0] == 'f') {
1818                                         bank_size = (int) argv[0]->f;
1819                                 } else {
1820                                         bank_size = argv[0]->i;
1821                                 }
1822                                 set_surface_port (port, msg);
1823                                 ret = set_surface (bank_size, strip_types, feedback, fadermode, se_page, pi_page, msg);
1824                                 if ((uint32_t) linkset != sur->linkset) {
1825                                         set_link (linkset, linkid, get_address (msg));
1826                                 }
1827                                 break;
1828                         case 0:
1829                                 // send current setup
1830                                 {
1831                                         lo_message reply = lo_message_new ();
1832                                         lo_message_add_int32 (reply, bank_size);
1833                                         lo_message_add_int32 (reply, strip_types);
1834                                         lo_message_add_int32 (reply, feedback);
1835                                         lo_message_add_int32 (reply, fadermode);
1836                                         lo_message_add_int32 (reply, se_page);
1837                                         lo_message_add_int32 (reply, pi_page);
1838                                         lo_message_add_int32 (reply, (int) linkset);
1839                                         lo_message_add_int32 (reply, (int) linkid);
1840                                         lo_message_add_int32 (reply, (int) port);
1841                                         lo_send_message (get_address (msg), X_("/set_surface"), reply);
1842                                         lo_message_free (reply);
1843                                         return 0;
1844                                 }
1845                                 break;
1846
1847                         default:
1848                                 PBD::warning << "OSC: Too many parameters." << endmsg;
1849                                 return 1;
1850                                 break;
1851                 }
1852         } else if (isdigit(path[13])) {
1853                 // some of our parameters must be "in-lined"
1854                 bank_size = atoi (&path[13]);
1855                 const char * par = strstr (&path[13], "/");
1856                 if (par) {
1857                         strip_types = atoi (&par[1]);
1858                         const char * fb = strstr (&par[1], "/");
1859                         if (fb) {
1860                                 feedback = atoi (&fb[1]);
1861                                 const char * fm = strstr (&fb[1], "/");
1862                                 if (fm) {
1863                                         fadermode = atoi (&fm[1]);
1864                                         const char * sp = strstr (&fm[1], "/");
1865                                         if (sp) {
1866                                                 se_page = atoi (&sp[1]);
1867                                                 const char * pp = strstr (&sp[1], "/");
1868                                                 if (pp) {
1869                                                         pi_page = atoi (&pp[1]);
1870                                                         const char * po = strstr (&pp[1], "/");
1871                                                         if (po) {
1872                                                                 port = atoi (&po[1]);
1873                                                                 const char * ls = strstr (&po[1], "/");
1874                                                                 if (ls) {
1875                                                                         linkset = atoi (&ls[1]);
1876                                                                         const char * li = strstr (&ls[1], "/");
1877                                                                         if (li) {
1878                                                                                 linkid = atoi (&li[1]);
1879                                                                         } else {
1880                                                                                 if (types[0] == 'f') {
1881                                                                                         linkid = (int) argv[0]->f;
1882                                                                                 } else if (types[0] == 'i') {
1883                                                                                         linkid = argv[0]->i;
1884                                                                                 }
1885                                                                         }
1886                                                                 } else {
1887                                                                         if (types[0] == 'f') {
1888                                                                                 linkset = (int) argv[0]->f;
1889                                                                         } else if (types[0] == 'i') {
1890                                                                                 linkset = argv[0]->i;
1891                                                                         }
1892                                                                 }
1893                                                         } else {
1894                                                                 if (types[0] == 'f') {
1895                                                                         port = (int) argv[0]->f;
1896                                                                 } else if (types[0] == 'i') {
1897                                                                         port = argv[0]->i;
1898                                                                 }
1899                                                         }
1900                                                 } else {
1901                                                         if (types[0] == 'f') {
1902                                                                 pi_page = (int) argv[0]->f;
1903                                                         } else if (types[0] == 'i') {
1904                                                                 pi_page = argv[0]->i;
1905                                                         }
1906                                                 }
1907                                         } else {
1908                                                 if (types[0] == 'f') {
1909                                                         se_page = (int) argv[0]->f;
1910                                                 } else if (types[0] == 'i') {
1911                                                         se_page = argv[0]->i;
1912                                                 }
1913                                         }
1914                                 } else {
1915                                         if (types[0] == 'f') {
1916                                                 fadermode = (int) argv[0]->f;
1917                                         } else if (types[0] == 'i') {
1918                                                 fadermode = argv[0]->i;
1919                                         }
1920                                 }
1921                         } else {
1922                                 if (types[0] == 'f') {
1923                                         feedback = (int) argv[0]->f;
1924                                 } else if (types[0] == 'i') {
1925                                         feedback = argv[0]->i;
1926                                 }
1927                         }
1928                 } else {
1929                         if (types[0] == 'f') {
1930                                 strip_types = (int) argv[0]->f;
1931                         } else if (types[0] == 'i') {
1932                                 strip_types = argv[0]->i;
1933                         }
1934                 }
1935                 set_surface_port (port, msg);
1936                 ret = set_surface (bank_size, strip_types, feedback, fadermode, se_page, pi_page, msg);
1937                 if ((uint32_t) linkset != sur->linkset) {
1938                         set_link (linkset, linkid, get_address (msg));
1939                 }
1940         }
1941         return ret;
1942 }
1943
1944 int
1945 OSC::set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gm, uint32_t se_size, uint32_t pi_size, lo_message msg)
1946 {
1947         if (observer_busy) {
1948                 return -1;
1949         }
1950         OSCSurface *s = get_surface(get_address (msg), true);
1951         s->bank_size = b_size;
1952         if (s->custom_mode && b_size) {
1953                 s->custom_mode = s->custom_mode | 0x4;
1954         }
1955         s->strip_types = strips;
1956         s->feedback = fb;
1957         s->gainmode = gm;
1958         if (s->strip_types[10]) {
1959                 s->usegroup = PBD::Controllable::UseGroup;
1960         } else {
1961                 s->usegroup = PBD::Controllable::NoGroup;
1962         }
1963         s->send_page_size = se_size;
1964         s->plug_page_size = pi_size;
1965         if (s->linkset) {
1966                 set_link (s->linkset, s->linkid, get_address (msg));
1967                 link_strip_types (s->linkset, s->strip_types.to_ulong());
1968         } else {
1969                 // set bank and strip feedback
1970                 strip_feedback(s, true);
1971                 _set_bank (1, get_address (msg));
1972         }
1973
1974         global_feedback (s);
1975         sel_send_pagesize (se_size, msg);
1976         sel_plug_pagesize (pi_size, msg);
1977         return 0;
1978 }
1979
1980 int
1981 OSC::set_surface_bank_size (uint32_t bs, lo_message msg)
1982 {
1983         if (observer_busy) {
1984                 return -1;
1985         }
1986         OSCSurface *s = get_surface(get_address (msg), true);
1987         s->bank_size = bs;
1988         if (s->custom_mode && bs) {
1989                 s->custom_mode = s->custom_mode | 0x4;
1990         }
1991         if (s->linkset) {
1992                 set_link (s->linkset, s->linkid, get_address (msg));
1993         } else {
1994                 // set bank and strip feedback
1995                 _set_bank (1, get_address (msg));
1996         }
1997         return 0;
1998 }
1999
2000 int
2001 OSC::set_surface_strip_types (uint32_t st, lo_message msg)
2002 {
2003         if (observer_busy) {
2004                 return -1;
2005         }
2006         OSCSurface *s = get_surface(get_address (msg), true);
2007         s->strip_types = st;
2008         if (s->strip_types[10]) {
2009                 s->usegroup = PBD::Controllable::UseGroup;
2010         } else {
2011                 s->usegroup = PBD::Controllable::NoGroup;
2012         }
2013         if (s->linkset) {
2014                 link_strip_types (s->linkset, st);
2015         }
2016
2017         // set bank and strip feedback
2018         _set_bank (1, get_address (msg));
2019         return 0;
2020 }
2021
2022
2023 int
2024 OSC::set_surface_feedback (uint32_t fb, lo_message msg)
2025 {
2026         if (observer_busy) {
2027                 return -1;
2028         }
2029         OSCSurface *s = get_surface(get_address (msg), true);
2030         s->feedback = fb;
2031
2032         strip_feedback (s, true);
2033         global_feedback (s);
2034         _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), get_address (msg));
2035         return 0;
2036 }
2037
2038 int
2039 OSC::set_surface_gainmode (uint32_t gm, lo_message msg)
2040 {
2041         if (observer_busy) {
2042                 return -1;
2043         }
2044         OSCSurface *s = get_surface(get_address (msg), true);
2045         s->gainmode = gm;
2046
2047         strip_feedback (s, true);
2048         global_feedback (s);
2049         _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), get_address (msg));
2050         return 0;
2051 }
2052
2053 int
2054 OSC::set_surface_port (uint32_t po, lo_message msg)
2055 {
2056         string new_port;
2057         if (!po) {
2058                 new_port = "auto";
2059         } else if (po > 1024) {
2060                 new_port = string_compose ("%1", po);
2061         } else {
2062                 PBD::warning << "Port value must be greater than 1024" << endmsg;
2063                 return -1;
2064         }
2065         OSCSurface *sur = get_surface(get_address (msg), true);
2066         lo_address addr = lo_message_get_source (msg);
2067         string host = lo_address_get_hostname (addr);
2068         string port = lo_address_get_port (addr);
2069         int protocol = lo_address_get_protocol (addr);
2070         for (uint32_t i = 0; i < _ports.size (); i++) {
2071                 if (_ports[i].host == host) {
2072                         if (_ports[i].port == new_port) {
2073                                 // no change - do nothing
2074                                 return 0;
2075                         } else {
2076                                 lo_address new_addr;
2077                                 _ports[i].port = new_port;
2078                                 if (new_port == "auto") {
2079                                         new_addr = addr;
2080                                 } else {
2081                                         new_addr = lo_address_new_with_proto (protocol, host.c_str(), new_port.c_str());
2082                                 }
2083                                 char * rurl;
2084                                 rurl = lo_address_get_url (new_addr);
2085                                 sur->remote_url = rurl;
2086                                 free (rurl);
2087                                 for (uint32_t it = 0; it < _surface.size();) {
2088                                         if (&_surface[it] == sur) {
2089                                                 it++;
2090                                                 continue;
2091                                         }
2092                                         char *sur_host = lo_url_get_hostname(_surface[it].remote_url.c_str());
2093                                         if (strstr (sur_host, host.c_str())) {
2094                                                 surface_destroy (&_surface[it]);
2095                                                 _surface.erase (_surface.begin() + it);
2096                                         } else {
2097                                                 it++;
2098                                         }
2099                                 }
2100                                 refresh_surface (msg);
2101                                 return 0;
2102                         }
2103                 }
2104         }
2105         // should not get here
2106         return -1;
2107 }
2108
2109 int
2110 OSC::check_surface (lo_message msg)
2111 {
2112         if (!session) {
2113                 return -1;
2114         }
2115         get_surface(get_address (msg));
2116         return 0;
2117 }
2118
2119 OSC::OSCSurface *
2120 OSC::get_surface (lo_address addr , bool quiet)
2121 {
2122         string r_url;
2123         char * rurl;
2124         rurl = lo_address_get_url (addr);
2125         r_url = rurl;
2126         free (rurl);
2127         for (uint32_t it = 0; it < _surface.size(); ++it) {
2128                 //find setup for this surface
2129                 if (!_surface[it].remote_url.find(r_url)){
2130                         return &_surface[it];
2131                 }
2132         }
2133
2134         // No surface create one with default values
2135         OSCSurface s;
2136         s.remote_url = r_url;
2137         s.no_clear = false;
2138         s.jogmode = 0;
2139         s.bank = 1;
2140         s.bank_size = default_banksize;
2141         s.observers.clear();
2142         s.sel_obs = 0;
2143         s.global_obs = 0;
2144         s.strip_types = default_strip;
2145         s.feedback = default_feedback;
2146         s.gainmode = default_gainmode;
2147         s.usegroup = PBD::Controllable::NoGroup;
2148         s.custom_strips.clear ();
2149         s.custom_mode = 0;
2150         s.sel_obs = 0;
2151         s.expand = 0;
2152         s.expand_enable = false;
2153         s.cue = false;
2154         s.aux = 0;
2155         s.cue_obs = 0;
2156         s.strips = get_sorted_stripables(s.strip_types, s.cue, false, s.custom_strips);
2157         s.send_page = 1;
2158         s.send_page_size = default_send_size;
2159         s.plug_page = 1;
2160         s.plug_page_size = default_plugin_size;
2161         s.plugin_id = 1;
2162         s.linkset = 0;
2163         s.linkid = 1;
2164
2165         s.nstrips = s.strips.size();
2166         {
2167                 _surface.push_back (s);
2168         }
2169
2170         if (!quiet) {
2171                 strip_feedback (&s, true);
2172                 global_feedback (&s);
2173                 _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), addr);
2174         }
2175
2176         return &_surface[_surface.size() - 1];
2177 }
2178
2179 // setup global feedback for a surface
2180 void
2181 OSC::global_feedback (OSCSurface* sur)
2182 {
2183         OSCGlobalObserver* o = sur->global_obs;
2184         delete o;
2185         if (sur->feedback[4] || sur->feedback[3] || sur->feedback[5] || sur->feedback[6]) {
2186
2187                 // create a new Global Observer for this surface
2188                 OSCGlobalObserver* o = new OSCGlobalObserver (*this, *session, sur);
2189                 sur->global_obs = o;
2190                 o->jog_mode (sur->jogmode);
2191         }
2192 }
2193
2194 void
2195 OSC::strip_feedback (OSCSurface* sur, bool new_bank_size)
2196 {
2197         LinkSet *set;
2198         uint32_t ls = sur->linkset;
2199
2200         if (ls) {
2201                 set = &(link_sets[ls]);
2202                 if (set->not_ready) {
2203                         return;
2204                 }
2205                 sur->custom_mode = set->custom_mode;
2206                 sur->custom_strips = set->custom_strips;
2207         }
2208         if (sur->custom_strips.size () == 0) {
2209                 sur->custom_mode = 0;
2210         }
2211         sur->strips = get_sorted_stripables(sur->strip_types, sur->cue, sur->custom_mode, sur->custom_strips);
2212         sur->nstrips = sur->strips.size();
2213         if (ls) {
2214                 set->strips = sur->strips;
2215         }
2216
2217         if (new_bank_size || (!sur->feedback[0] && !sur->feedback[1])) {
2218                 // delete old observers
2219                 for (uint32_t i = 0; i < sur->observers.size(); i++) {
2220                         if (!sur->bank_size) {
2221                                 sur->observers[i]->clear_strip ();
2222                         }
2223                         delete sur->observers[i];
2224                 }
2225                 sur->observers.clear();
2226
2227                 uint32_t bank_size = sur->bank_size;
2228                 if (!bank_size) {
2229                         bank_size = sur->nstrips;
2230                 }
2231
2232                 if (sur->feedback[0] || sur->feedback[1]) {
2233                         for (uint32_t i = 0; i < bank_size; i++) {
2234                                 OSCRouteObserver* o = new OSCRouteObserver (*this, i + 1, sur);
2235                                 sur->observers.push_back (o);
2236                         }
2237                 }
2238         } else {
2239                 if (sur->feedback[0] || sur->feedback[1]) {
2240                         for (uint32_t i = 0; i < sur->observers.size(); i++) {
2241                                 boost::shared_ptr<ARDOUR::Stripable> str = get_strip (i + 1, lo_address_new_from_url (sur->remote_url.c_str()));
2242                                 sur->observers[i]->refresh_strip(str, true);
2243                         }
2244                 }
2245         }
2246         bank_leds (sur);
2247 }
2248
2249 void
2250 OSC::notify_routes_added (ARDOUR::RouteList &)
2251 {
2252         // not sure if we need this PI change seems to cover
2253         //recalcbanks();
2254 }
2255
2256 void
2257 OSC::notify_vca_added (ARDOUR::VCAList &)
2258 {
2259         // not sure if we need this PI change seems to cover
2260         //recalcbanks();
2261 }
2262
2263 void
2264 OSC::recalcbanks ()
2265 {
2266         tick = false;
2267         bank_dirty = true;
2268 }
2269
2270 void
2271 OSC::_recalcbanks ()
2272 {
2273         if (observer_busy) {
2274                 return;
2275         }
2276         /*
2277          * We have two different ways of working here:
2278          * 1) banked: The controller has a bank of strips and only can deal
2279          * with banksize strips. We start banksize observers which run until
2280          * either banksize is changed or Ardour exits.
2281          *
2282          * 2) banksize is 0 or unlimited and so is the same size as the number
2283          * of strips. For a recalc, We want to tear down all strips but not send
2284          * a reset value for any of the controls and then rebuild all observers.
2285          * this is easier than detecting change in "bank" size and deleting or
2286          * adding just a few.
2287          */
2288
2289         // refresh each surface we know about.
2290         for (uint32_t it = 0; it < _surface.size(); ++it) {
2291                 OSCSurface* sur = &_surface[it];
2292                 // find lo_address
2293                 lo_address addr = lo_address_new_from_url (sur->remote_url.c_str());
2294                 if (sur->cue) {
2295                         _cue_set (sur->aux, addr);
2296                 } else if (!sur->bank_size) {
2297                         strip_feedback (sur, true);
2298                         // This surface uses /strip/list tell it routes have changed
2299                         lo_message reply;
2300                         reply = lo_message_new ();
2301                         lo_send_message (addr, X_("/strip/list"), reply);
2302                         lo_message_free (reply);
2303                 } else {
2304                         strip_feedback (sur, false);
2305                 }
2306                 _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), addr);
2307         }
2308 }
2309
2310 int
2311 OSC::set_bank (uint32_t bank_start, lo_message msg)
2312 {
2313         return _set_bank (bank_start, get_address (msg));
2314 }
2315
2316 // set bank is callable with either message or address
2317 int
2318 OSC::_set_bank (uint32_t bank_start, lo_address addr)
2319 {
2320         if (!session) {
2321                 return -1;
2322         }
2323         if (!session->nroutes()) {
2324                 return -1;
2325         }
2326
2327         OSCSurface *s = get_surface (addr, true);
2328
2329         Sorted striplist = s->strips;
2330         uint32_t nstrips = s->nstrips;
2331
2332         LinkSet *set;
2333         uint32_t ls = s->linkset;
2334
2335         if (ls) {
2336                 //we have a linkset... deal with each surface
2337                 set = &(link_sets[ls]);
2338                 if (set->not_ready) {
2339                         return 1;
2340                 }
2341                 uint32_t d_count = set->urls.size();
2342                 set->strips = striplist;
2343                 bank_start = bank_limits_check (bank_start, set->banksize, nstrips);
2344                 set->bank = bank_start;
2345                 uint32_t not_ready = 0;
2346                 for (uint32_t dv = 1; dv < d_count; dv++) {
2347                         OSCSurface *sur;
2348                         if (set->urls[dv] != "") {
2349                                 string url = set->urls[dv];
2350                                 sur = get_surface (lo_address_new_from_url (url.c_str()));
2351                         } else {
2352                                 not_ready = dv;
2353                         }
2354                         if (sur->linkset != ls) {
2355                                 set->urls[dv] = "";
2356                                 not_ready = dv;
2357                         }
2358                         if (not_ready) {
2359                                 if (!set->not_ready) {
2360                                         set->not_ready = not_ready;
2361                                 }
2362                                 set->bank = 1;
2363                                 break;
2364                         }
2365                         lo_address sur_addr = lo_address_new_from_url (sur->remote_url.c_str());
2366
2367                         sur->bank = bank_start;
2368                         bank_start = bank_start + sur->bank_size;
2369                         strip_feedback (sur, false);
2370                         _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), sur_addr);
2371                         bank_leds (sur);
2372                         lo_address_free (sur_addr);
2373                 }
2374                 if (not_ready) {
2375                         surface_link_state (set);
2376                 }
2377
2378         } else {
2379
2380                 s->bank = bank_limits_check (bank_start, s->bank_size, nstrips);
2381                 strip_feedback (s, true);
2382                 _strip_select (boost::shared_ptr<ARDOUR::Stripable>(), addr);
2383                 bank_leds (s);
2384         }
2385
2386
2387         bank_dirty = false;
2388         tick = true;
2389         return 0;
2390 }
2391
2392 uint32_t
2393 OSC::bank_limits_check (uint32_t bank, uint32_t size, uint32_t total)
2394 {
2395         uint32_t b_size;
2396         if (!size) {
2397                 // no banking - bank includes all stripables
2398                 b_size = total;
2399         } else {
2400                 b_size = size;
2401         }
2402         // Do limits checking
2403         if (bank < 1) bank = 1;
2404         if (b_size >= total)  {
2405                 bank = 1;
2406         } else if (bank > ((total - b_size) + 1)) {
2407                 // top bank is always filled if there are enough strips for at least one bank
2408                 bank = (uint32_t)((total - b_size) + 1);
2409         }
2410         return bank;
2411 }
2412
2413 void
2414 OSC::bank_leds (OSCSurface* s)
2415 {
2416         uint32_t bank = 0;
2417         uint32_t size = 0;
2418         uint32_t total = 0;
2419         // light bankup or bankdown buttons if it is possible to bank in that direction
2420         lo_address addr = lo_address_new_from_url (s->remote_url.c_str());
2421         if (s->linkset) {
2422                 LinkSet *set;
2423                 set = &(link_sets[s->linkset]);
2424                 bank = set->bank;
2425                 size = set->banksize;
2426                 total = s->nstrips;
2427                 if (set->not_ready) {
2428                         total = 1;
2429                 }
2430         } else {
2431                 bank = s->bank;
2432                 size = s->bank_size;
2433                 total = s->nstrips;
2434         }
2435         if (size && (s->feedback[0] || s->feedback[1] || s->feedback[4])) {
2436                 lo_message reply;
2437                 reply = lo_message_new ();
2438                 if ((total <= size) || (bank > (total - size))) {
2439                         lo_message_add_int32 (reply, 0);
2440                 } else {
2441                         lo_message_add_int32 (reply, 1);
2442                 }
2443                 lo_send_message (addr, X_("/bank_up"), reply);
2444                 lo_message_free (reply);
2445                 reply = lo_message_new ();
2446                 if (bank > 1) {
2447                         lo_message_add_int32 (reply, 1);
2448                 } else {
2449                         lo_message_add_int32 (reply, 0);
2450                 }
2451                 lo_send_message (addr, X_("/bank_down"), reply);
2452                 lo_message_free (reply);
2453         }
2454 }
2455
2456 int
2457 OSC::bank_up (lo_message msg)
2458 {
2459         return bank_delta (1.0, msg);
2460 }
2461
2462 int
2463 OSC::bank_delta (float delta, lo_message msg)
2464 {
2465         if (!session) {
2466                 return -1;
2467         }
2468         // only do deltas of -1 0 or 1
2469         if (delta > 0) {
2470                 delta = 1;
2471         } else if (delta < 0) {
2472                 delta = -1;
2473         } else {
2474                 // 0  key release ignore
2475                 return 0;
2476         }
2477         OSCSurface *s = get_surface(get_address (msg));
2478         if (!s->bank_size) {
2479                 // bank size of 0 means use all strips no banking
2480                 return 0;
2481         }
2482         uint32_t old_bank = 0;
2483         uint32_t bank_size = 0;
2484         if (s->linkset) {
2485                 old_bank = link_sets[s->linkset].bank;
2486                 bank_size = link_sets[s->linkset].banksize;
2487         } else {
2488                 old_bank = s->bank;
2489                 bank_size = s->bank_size;
2490         }
2491         uint32_t new_bank = old_bank + (bank_size * (int) delta);
2492         if ((int)new_bank < 1) {
2493                 new_bank = 1;
2494         }
2495         if (new_bank != old_bank) {
2496                 set_bank (new_bank, msg);
2497         }
2498         return 0;
2499 }
2500
2501 int
2502 OSC::bank_down (lo_message msg)
2503 {
2504         return bank_delta (-1.0, msg);
2505 }
2506
2507 int
2508 OSC::use_group (float value, lo_message msg)
2509 {
2510         if (!session) {
2511                 return -1;
2512         }
2513         OSCSurface *s = get_surface(get_address (msg));
2514         if (value) {
2515                 s->usegroup = PBD::Controllable::UseGroup;
2516         } else {
2517                 s->usegroup = PBD::Controllable::NoGroup;
2518         }
2519         return 0;
2520 }
2521
2522 uint32_t
2523 OSC::get_sid (boost::shared_ptr<ARDOUR::Stripable> strip, lo_address addr)
2524 {
2525         if (!strip) {
2526                 return 0;
2527         }
2528
2529         OSCSurface *s = get_surface(addr);
2530
2531         uint32_t b_size;
2532         if (!s->bank_size) {
2533                 // no banking
2534                 b_size = s->nstrips;
2535         } else {
2536                 b_size = s->bank_size;
2537         }
2538
2539         for (uint32_t n = s->bank; n < (min ((b_size + s->bank), s->nstrips + 1)); ++n) {
2540                 if (n <= s->strips.size()) {
2541                         if (strip == s->strips[n-1]) {
2542                                 return n - s->bank + 1;
2543                         }
2544                 }
2545         }
2546         // failsafe... should never get here.
2547         return 0;
2548 }
2549
2550 boost::shared_ptr<ARDOUR::Stripable>
2551 OSC::get_strip (uint32_t ssid, lo_address addr)
2552 {
2553         OSCSurface *s = get_surface(addr);
2554         if (ssid && ((ssid + s->bank - 2) < s->nstrips)) {
2555                 return s->strips[ssid + s->bank - 2];
2556         }
2557         // guess it is out of range
2558         return boost::shared_ptr<ARDOUR::Stripable>();
2559 }
2560
2561 // send and plugin paging commands
2562 int
2563 OSC::sel_send_pagesize (uint32_t size, lo_message msg)
2564 {
2565         OSCSurface *s = get_surface(get_address (msg));
2566         if  (size != s->send_page_size) {
2567                 s->send_page_size = size;
2568                 s->sel_obs->set_send_size(size);
2569         }
2570         return 0;
2571 }
2572
2573 int
2574 OSC::sel_send_page (int page, lo_message msg)
2575 {
2576         OSCSurface *s = get_surface(get_address (msg));
2577         uint32_t send_size = s->send_page_size;
2578         if (!send_size) {
2579                 send_size = s->nsends;
2580         }
2581         uint32_t max_page = (uint32_t)(s->nsends / send_size) + 1;
2582         s->send_page = s->send_page + page;
2583         if (s->send_page < 1) {
2584                 s->send_page = 1;
2585         } else if ((uint32_t)s->send_page > max_page) {
2586                 s->send_page = max_page;
2587         }
2588         s->sel_obs->set_send_page (s->send_page);
2589         return 0;
2590 }
2591
2592 int
2593 OSC::sel_plug_pagesize (uint32_t size, lo_message msg)
2594 {
2595         OSCSurface *s = get_surface(get_address (msg));
2596         if (size != s->plug_page_size) {
2597                 s->plug_page_size = size;
2598                 s->sel_obs->set_plugin_size(size);
2599         }
2600         return 0;
2601 }
2602
2603 int
2604 OSC::sel_plug_page (int page, lo_message msg)
2605 {
2606         if (!page) {
2607                 return 0;
2608         }
2609         int new_page = 0;
2610         OSCSurface *s = get_surface(get_address (msg));
2611         if (page > 0) {
2612                 new_page = s->plug_page + s->plug_page_size;
2613                 if ((uint32_t) new_page > s->plug_params.size ()) {
2614                         new_page = s->plug_page;
2615                 }
2616         } else {
2617                 new_page = s->plug_page - s->plug_page_size;
2618                 if (new_page < 1) {
2619                         new_page = 1;
2620                 }
2621         }
2622         if (new_page != s->plug_page) {
2623                 s->plug_page = new_page;
2624                 s->sel_obs->set_plugin_page(s->plug_page);
2625         }
2626         return 0;
2627 }
2628
2629 int
2630 OSC::sel_plugin (int delta, lo_message msg)
2631 {
2632         if (!delta) {
2633                 return 0;
2634         }
2635         OSCSurface *sur = get_surface(get_address (msg));
2636         return _sel_plugin (sur->plugin_id + delta, get_address (msg));
2637 }
2638
2639 int
2640 OSC::_sel_plugin (int id, lo_address addr)
2641 {
2642         OSCSurface *sur = get_surface(addr);
2643         boost::shared_ptr<Stripable> s = sur->select;
2644         if (s) {
2645                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
2646                 if (!r) {
2647                         return 1;
2648                 }
2649
2650                 // find out how many plugins we have
2651                 bool plugs;
2652                 int nplugs  = 0;
2653                 sur->plugins.clear();
2654                 do {
2655                         plugs = false;
2656                         if (r->nth_plugin (nplugs)) {
2657                                 if (r->nth_plugin(nplugs)->display_to_user()) {
2658 #ifdef MIXBUS
2659                                         // need to check for mixbus channel strips (and exclude them)
2660                                         boost::shared_ptr<Processor> proc = r->nth_plugin (nplugs);
2661                                         boost::shared_ptr<PluginInsert> pi;
2662                                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
2663
2664                                                 if (!pi->is_channelstrip()) {
2665 #endif
2666                                                         sur->plugins.push_back (nplugs);
2667                                                         nplugs++;
2668 #ifdef MIXBUS
2669                                                 }
2670                                         }
2671 #endif
2672                                 }
2673                                 plugs = true;
2674                         }
2675                 } while (plugs);
2676
2677                 // limit plugin_id to actual plugins
2678                 if (sur->plugins.size() < 1) {
2679                         sur->plugin_id = 0;
2680                         sur->plug_page = 1;
2681                         if (sur->sel_obs) {
2682                                 sur->sel_obs->set_plugin_id(-1, 1);
2683                         }
2684                         return 0;
2685                 } else if (id < 1) {
2686                         sur->plugin_id = 1;
2687                 } else if (sur->plugins.size() < (uint32_t) id) {
2688                         sur->plugin_id = sur->plugins.size();
2689                 } else {
2690                         sur->plugin_id = id;
2691                 }
2692
2693                 // we have a plugin number now get the processor
2694                 boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
2695                 boost::shared_ptr<PluginInsert> pi;
2696                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
2697                         PBD::warning << "OSC: Plugin: " << sur->plugin_id << " does not seem to be a plugin" << endmsg;                 
2698                         return 1;
2699                 }
2700                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
2701                 bool ok = false;
2702                 // put only input controls into a vector
2703                 sur->plug_params.clear ();
2704                 uint32_t nplug_params  = pip->parameter_count();
2705                 for ( uint32_t ppi = 0;  ppi < nplug_params; ++ppi) {
2706                         uint32_t controlid = pip->nth_parameter(ppi, ok);
2707                         if (!ok) {
2708                                 continue;
2709                         }
2710                         if (pip->parameter_is_input(controlid)) {
2711                                 sur->plug_params.push_back (ppi);
2712                         }
2713                 }
2714
2715                 sur->plug_page = 1;
2716
2717                 if (sur->sel_obs) {
2718                         sur->sel_obs->set_plugin_id(sur->plugins[sur->plugin_id - 1], sur->plug_page);
2719                 }
2720                 return 0;
2721         }
2722         return 1;
2723 }
2724
2725 void
2726 OSC::transport_sample (lo_message msg)
2727 {
2728         if (!session) {
2729                 return;
2730         }
2731         check_surface (msg);
2732         samplepos_t pos = session->transport_sample ();
2733
2734         lo_message reply = lo_message_new ();
2735         lo_message_add_int64 (reply, pos);
2736
2737         lo_send_message (get_address (msg), X_("/transport_frame"), reply);
2738
2739         lo_message_free (reply);
2740 }
2741
2742 void
2743 OSC::transport_speed (lo_message msg)
2744 {
2745         if (!session) {
2746                 return;
2747         }
2748         check_surface (msg);
2749         double ts = session->transport_speed ();
2750
2751         lo_message reply = lo_message_new ();
2752         lo_message_add_double (reply, ts);
2753
2754         lo_send_message (get_address (msg), X_("/transport_speed"), reply);
2755
2756         lo_message_free (reply);
2757 }
2758
2759 void
2760 OSC::record_enabled (lo_message msg)
2761 {
2762         if (!session) {
2763                 return;
2764         }
2765         check_surface (msg);
2766         int re = (int)session->get_record_enabled ();
2767
2768         lo_message reply = lo_message_new ();
2769         lo_message_add_int32 (reply, re);
2770
2771         lo_send_message (get_address (msg), X_("/record_enabled"), reply);
2772
2773         lo_message_free (reply);
2774 }
2775
2776 int
2777 OSC::scrub (float delta, lo_message msg)
2778 {
2779         if (!session) return -1;
2780         check_surface (msg);
2781
2782         scrub_place = session->transport_sample ();
2783
2784         float speed;
2785
2786         int64_t now = ARDOUR::get_microseconds ();
2787         int64_t diff = now - scrub_time;
2788         if (diff > 35000) {
2789                 // speed 1 (or 0 if jog wheel supports touch)
2790                 speed = delta;
2791         } else if ((diff > 20000) && (fabs(scrub_speed) == 1)) {
2792                 // add some hysteresis to stop excess speed jumps
2793                 speed = delta;
2794         } else {
2795                 speed = (int)(delta * 2);
2796         }
2797         scrub_time = now;
2798         if (scrub_speed == speed) {
2799                 // Already at that speed no change
2800                 return 0;
2801         }
2802         scrub_speed = speed;
2803
2804         if (speed > 0) {
2805                 if (speed == 1) {
2806                         session->request_transport_speed (.5);
2807                 } else {
2808                         session->request_transport_speed (9.9);
2809                 }
2810         } else if (speed < 0) {
2811                 if (speed == -1) {
2812                         session->request_transport_speed (-.5);
2813                 } else {
2814                         session->request_transport_speed (-1);
2815                 }
2816         } else {
2817                 session->request_transport_speed (0);
2818         }
2819
2820         return 0;
2821 }
2822
2823 int
2824 OSC::jog (float delta, lo_message msg)
2825 {
2826         if (!session) return -1;
2827
2828         OSCSurface *s = get_surface(get_address (msg));
2829
2830         switch(s->jogmode)
2831         {
2832                 case 0:
2833                         if (delta) {
2834                                 jump_by_seconds (delta / 5);
2835                         }
2836                         break;
2837                 case 1:
2838                         if (delta > 0) {
2839                                 access_action ("Common/nudge-playhead-forward");
2840                         } else if (delta < 0) {
2841                                 access_action ("Common/nudge-playhead-backward");
2842                         }
2843                         break;
2844                 case 2:
2845                         scrub (delta, msg);
2846                         break;
2847                 case 3:
2848                         if (delta) {
2849                                 double speed = get_transport_speed ();
2850                                 set_transport_speed (speed + (delta / 8.1));
2851                         } else {
2852                                 set_transport_speed (0);
2853                         }
2854                         break;
2855                 case 4:
2856                         if (delta > 0) {
2857                                 next_marker ();
2858                         } else if (delta < 0) {
2859                                 prev_marker ();
2860                         }
2861                         break;
2862                 case 5:
2863                         if (delta > 0) {
2864                                 access_action ("Editor/scroll-forward");
2865                         } else if (delta < 0) {
2866                                 access_action ("Editor/scroll-backward");
2867                         }
2868                         break;
2869                 case 6:
2870                         if (delta > 0) {
2871                                 set_bank (s->bank + 1, msg);
2872                         } else if (delta < 0) {
2873                                 set_bank (s->bank - 1, msg);
2874                         }
2875                         break;
2876                 case 7:
2877                         if (delta > 0) {
2878                                 bank_up (msg);
2879                         } else if (delta < 0) {
2880                                 bank_down (msg);
2881                         }
2882                         break;
2883                 default:
2884                         break;
2885
2886         }
2887         return 0;
2888
2889 }
2890
2891 int
2892 OSC::jog_mode (float mode, lo_message msg)
2893 {
2894         if (!session) return -1;
2895
2896         OSCSurface *s = get_surface(get_address (msg));
2897         if (get_transport_speed () != 1.0) {
2898                 set_transport_speed (0);
2899         }
2900         s->jogmode = (uint32_t) mode;
2901         s->global_obs->jog_mode (mode);
2902         return 0;
2903 }
2904
2905 // two structs to help with going to markers
2906 struct LocationMarker {
2907         LocationMarker (const std::string& l, samplepos_t w)
2908                 : label (l), when (w) {}
2909         std::string label;
2910         samplepos_t  when;
2911 };
2912
2913 struct LocationMarkerSort {
2914         bool operator() (const LocationMarker& a, const LocationMarker& b) {
2915                 return (a.when < b.when);
2916         }
2917 };
2918
2919 int
2920 OSC::set_marker (const char* types, lo_arg **argv, int argc, lo_message msg)
2921 {
2922         if (argc != 1) {
2923                 PBD::warning << "Wrong number of parameters, one only." << endmsg;
2924                 return -1;
2925         }
2926         const Locations::LocationList& ll (session->locations ()->list ());
2927         uint32_t marker = 0;
2928
2929         switch (types[0]) {
2930                 case 's':
2931                         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
2932                                 if ((*l)->is_mark ()) {
2933                                         if (strcmp (&argv[0]->s, (*l)->name().c_str()) == 0) {
2934                                                 session->request_locate ((*l)->start (), false);
2935                                                 return 0;
2936                                         }
2937                                 }
2938                         }
2939                         break;
2940                 case 'i':
2941                         marker = (uint32_t) argv[0]->i - 1;
2942                         break;
2943                 case 'f':
2944                         marker = (uint32_t) argv[0]->f - 1;
2945                         break;
2946                 default:
2947                         return -1;
2948                         break;
2949         }
2950         std::vector<LocationMarker> lm;
2951         // get Locations that are marks
2952         for (Locations::LocationList::const_iterator l = ll.begin(); l != ll.end(); ++l) {
2953                 if ((*l)->is_mark ()) {
2954                         lm.push_back (LocationMarker((*l)->name(), (*l)->start ()));
2955                 }
2956         }
2957         // sort them by position
2958         LocationMarkerSort location_marker_sort;
2959         std::sort (lm.begin(), lm.end(), location_marker_sort);
2960         // go there
2961         if (marker < lm.size()) {
2962                 session->request_locate (lm[marker].when, false);
2963                 return 0;
2964         }
2965         // we were unable to deal with things
2966         return -1;
2967 }
2968
2969 int
2970 OSC::group_list (lo_message msg)
2971 {
2972         return send_group_list (get_address (msg));
2973 }
2974
2975 int
2976 OSC::send_group_list (lo_address addr)
2977 {
2978                 //std::list<RouteGroup*> const & route_groups () const {
2979         lo_message reply;
2980         reply = lo_message_new ();
2981
2982         lo_message_add_string (reply, X_("none"));
2983
2984         std::list<RouteGroup*> groups = session->route_groups ();
2985         for (std::list<RouteGroup *>::iterator i = groups.begin(); i != groups.end(); ++i) {
2986                 RouteGroup *rg = *i;
2987                 lo_message_add_string (reply, rg->name().c_str());
2988         }
2989         lo_send_message (addr, X_("/group/list"), reply);
2990         lo_message_free (reply);
2991         return 0;
2992 }
2993
2994 int
2995 OSC::click_level (float position)
2996 {
2997         if (!session) return -1;
2998         if (session->click_gain()->gain_control()) {
2999                 session->click_gain()->gain_control()->set_value (session->click_gain()->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
3000         }
3001         return 0;
3002 }
3003
3004 // master and monitor calls
3005 int
3006 OSC::master_set_gain (float dB)
3007 {
3008         if (!session) return -1;
3009         boost::shared_ptr<Stripable> s = session->master_out();
3010         if (s) {
3011                 if (dB < -192) {
3012                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
3013                 } else {
3014                         float abs = dB_to_coefficient (dB);
3015                         float top = s->gain_control()->upper();
3016                         if (abs > top) {
3017                                 abs = top;
3018                         }
3019                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3020                 }
3021         }
3022         return 0;
3023 }
3024
3025 int
3026 OSC::master_delta_gain (float delta)
3027 {
3028         if (!session) return -1;
3029         boost::shared_ptr<Stripable> s = session->master_out();
3030         if (s) {
3031                 float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
3032                 if (dB < -192) {
3033                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
3034                 } else {
3035                         float abs = dB_to_coefficient (dB);
3036                         float top = s->gain_control()->upper();
3037                         if (abs > top) {
3038                                 abs = top;
3039                         }
3040                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3041                 }
3042         }
3043         return 0;
3044 }
3045
3046 int
3047 OSC::master_set_fader (float position)
3048 {
3049         if (!session) return -1;
3050         boost::shared_ptr<Stripable> s = session->master_out();
3051         if (s) {
3052                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
3053         }
3054         return 0;
3055 }
3056
3057 int
3058 OSC::master_set_trim (float dB)
3059 {
3060         if (!session) return -1;
3061         boost::shared_ptr<Stripable> s = session->master_out();
3062
3063         if (s) {
3064                 s->trim_control()->set_value (dB_to_coefficient (dB), PBD::Controllable::NoGroup);
3065         }
3066
3067         return 0;
3068 }
3069
3070 int
3071 OSC::master_set_pan_stereo_position (float position, lo_message msg)
3072 {
3073         if (!session) return -1;
3074         OSCSurface *sur = get_surface(get_address (msg));
3075
3076         float endposition = .5;
3077         boost::shared_ptr<Stripable> s = session->master_out();
3078
3079         if (s) {
3080                 if (s->pan_azimuth_control()) {
3081                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
3082                         endposition = s->pan_azimuth_control()->internal_to_interface (s->pan_azimuth_control()->get_value ());
3083                 }
3084         }
3085
3086         if (sur->feedback[4]) {
3087                 lo_message reply = lo_message_new ();
3088                 lo_message_add_float (reply, endposition);
3089
3090                 lo_send_message (get_address (msg), X_("/master/pan_stereo_position"), reply);
3091                 lo_message_free (reply);
3092         }
3093
3094         return 0;
3095 }
3096
3097 int
3098 OSC::master_set_mute (uint32_t state)
3099 {
3100         if (!session) return -1;
3101
3102         boost::shared_ptr<Stripable> s = session->master_out();
3103
3104         if (s) {
3105                 s->mute_control()->set_value (state, PBD::Controllable::NoGroup);
3106         }
3107
3108         return 0;
3109 }
3110
3111 int
3112 OSC::master_select (lo_message msg)
3113 {
3114         if (!session) {
3115                 return -1;
3116         }
3117         OSCSurface *sur = get_surface(get_address (msg));
3118         sur->expand_enable = false;
3119         boost::shared_ptr<Stripable> s = session->master_out();
3120         if (s) {
3121                 SetStripableSelection (s);
3122         }
3123
3124         return 0;
3125 }
3126
3127 int
3128 OSC::monitor_set_gain (float dB)
3129 {
3130         if (!session) return -1;
3131         boost::shared_ptr<Stripable> s = session->monitor_out();
3132
3133         if (s) {
3134                 if (dB < -192) {
3135                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
3136                 } else {
3137                         float abs = dB_to_coefficient (dB);
3138                         float top = s->gain_control()->upper();
3139                         if (abs > top) {
3140                                 abs = top;
3141                         }
3142                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3143                 }
3144         }
3145         return 0;
3146 }
3147
3148 int
3149 OSC::monitor_delta_gain (float delta)
3150 {
3151         if (!session) return -1;
3152         boost::shared_ptr<Stripable> s = session->monitor_out();
3153         if (s) {
3154                 float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
3155                 if (dB < -192) {
3156                         s->gain_control()->set_value (0.0, PBD::Controllable::NoGroup);
3157                 } else {
3158                         float abs = dB_to_coefficient (dB);
3159                         float top = s->gain_control()->upper();
3160                         if (abs > top) {
3161                                 abs = top;
3162                         }
3163                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
3164                 }
3165         }
3166         return 0;
3167 }
3168
3169 int
3170 OSC::monitor_set_fader (float position)
3171 {
3172         if (!session) return -1;
3173         boost::shared_ptr<Stripable> s = session->monitor_out();
3174         if (s) {
3175                 s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
3176         }
3177         return 0;
3178 }
3179
3180 int
3181 OSC::monitor_set_mute (uint32_t state)
3182 {
3183         if (!session) return -1;
3184
3185         if (session->monitor_out()) {
3186                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
3187                 mon->set_cut_all (state);
3188         }
3189         return 0;
3190 }
3191
3192 int
3193 OSC::monitor_set_dim (uint32_t state)
3194 {
3195         if (!session) return -1;
3196
3197         if (session->monitor_out()) {
3198                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
3199                 mon->set_dim_all (state);
3200         }
3201         return 0;
3202 }
3203
3204 int
3205 OSC::monitor_set_mono (uint32_t state)
3206 {
3207         if (!session) return -1;
3208
3209         if (session->monitor_out()) {
3210                 boost::shared_ptr<MonitorProcessor> mon = session->monitor_out()->monitor_control();
3211                 mon->set_mono (state);
3212         }
3213         return 0;
3214 }
3215
3216 int
3217 OSC::route_get_sends(lo_message msg) {
3218         if (!session) {
3219                 return -1;
3220         }
3221
3222         lo_arg **argv = lo_message_get_argv(msg);
3223
3224         int rid = argv[0]->i;
3225
3226         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
3227         if (!strip) {
3228                 return -1;
3229         }
3230
3231         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
3232         if (!r) {
3233                 return -1;
3234         }
3235
3236         lo_message reply = lo_message_new();
3237         lo_message_add_int32(reply, rid);
3238
3239         int i = 0;
3240         for (;;) {
3241                 boost::shared_ptr<Processor> p = r->nth_send(i++);
3242
3243                 if (!p) {
3244                         break;
3245                 }
3246
3247                 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
3248                 if (isend) {
3249                         lo_message_add_int32(reply, get_sid(isend->target_route(), get_address(msg)));
3250                         lo_message_add_string(reply, isend->name().c_str());
3251                         lo_message_add_int32(reply, i);
3252                         boost::shared_ptr<Amp> a = isend->amp();
3253                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
3254                         lo_message_add_int32(reply, p->active() ? 1 : 0);
3255                 }
3256         }
3257         // if used dedicated message path to identify this reply in async operation.
3258         // Naming it #reply wont help the client to identify the content.
3259         lo_send_message(get_address (msg), X_("/strip/sends"), reply);
3260
3261         lo_message_free(reply);
3262
3263         return 0;
3264 }
3265
3266 int
3267 OSC::route_get_receives(lo_message msg) {
3268         if (!session) {
3269                 return -1;
3270         }
3271
3272         lo_arg **argv = lo_message_get_argv(msg);
3273
3274         uint32_t rid = argv[0]->i;
3275
3276
3277         boost::shared_ptr<Stripable> strip = get_strip(rid, get_address(msg));
3278         if (!strip) {
3279                 return -1;
3280         }
3281
3282         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (strip);
3283         if (!r) {
3284                 return -1;
3285         }
3286
3287         boost::shared_ptr<RouteList> route_list = session->get_routes();
3288
3289         lo_message reply = lo_message_new();
3290         lo_message_add_int32(reply, rid);
3291
3292         for (RouteList::iterator i = route_list->begin(); i != route_list->end(); ++i) {
3293                 boost::shared_ptr<Route> tr = boost::dynamic_pointer_cast<Route> (*i);
3294                 if (!tr) {
3295                         continue;
3296                 }
3297                 int j = 0;
3298
3299                 for (;;) {
3300                         boost::shared_ptr<Processor> p = tr->nth_send(j++);
3301
3302                         if (!p) {
3303                                 break;
3304                         }
3305
3306                         boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (p);
3307                         if (isend) {
3308                                 if( isend->target_route()->id() == r->id()){
3309                                         boost::shared_ptr<Amp> a = isend->amp();
3310
3311                                         lo_message_add_int32(reply, get_sid(tr, get_address(msg)));
3312                                         lo_message_add_string(reply, tr->name().c_str());
3313                                         lo_message_add_int32(reply, j);
3314                                         lo_message_add_float(reply, a->gain_control()->internal_to_interface (a->gain_control()->get_value()));
3315                                         lo_message_add_int32(reply, p->active() ? 1 : 0);
3316                                 }
3317                         }
3318                 }
3319         }
3320
3321         // I have used a dedicated message path to identify this reply in async operation.
3322         // Naming it #reply wont help the client to identify the content.
3323         lo_send_message(get_address (msg), X_("/strip/receives"), reply);
3324         lo_message_free(reply);
3325         return 0;
3326 }
3327
3328 // strip calls
3329
3330 int
3331 OSC::set_automation (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
3332 {
3333         if (!session) return -1;
3334
3335         int ret = 1;
3336         OSCSurface *sur = get_surface(get_address (msg));
3337         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
3338         uint32_t ctr = 0;
3339         uint32_t aut = 0;
3340         uint32_t ssid;
3341
3342         if (argc) {
3343                 if (types[argc - 1] == 'f') {
3344                         aut = (int)argv[argc - 1]->f;
3345                 } else {
3346                         aut = argv[argc - 1]->i;
3347                 }
3348         }
3349
3350         //parse path first to find stripable
3351         if (!strncmp (path, X_("/strip/"), 7)) {
3352                 // find ssid and stripable
3353                 if (argc > 1) {
3354                         if (types[1] == 'f') {
3355                                 ssid = (uint32_t)argv[0]->f;
3356                         } else {
3357                                 ssid = argv[0]->i;
3358                         }
3359                         strp = get_strip (ssid, get_address (msg));
3360                 } else {
3361                         ssid = atoi (&(strrchr (path, '/' ))[1]);
3362                         strp = get_strip (ssid, get_address (msg));
3363                 }
3364                 ctr = 7;
3365         } else if (!strncmp (path, X_("/select/"), 8)) {
3366                 if (sur->expand_enable && sur->expand) {
3367                         strp = get_strip (sur->expand, get_address (msg));
3368                 } else {
3369                         strp = _select;
3370                 }
3371                 ctr = 8;
3372         } else {
3373                 return ret;
3374         }
3375         if (strp) {
3376                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
3377                 // other automatable controls can be added by repeating the next 6.5 lines
3378                 if ((!strncmp (&path[ctr], X_("fader"), 5)) || (!strncmp (&path[ctr], X_("gain"), 4))) {
3379                         if (strp->gain_control ()) {
3380                                 control = strp->gain_control ();
3381                         } else {
3382                                 PBD::warning << "No fader for this strip" << endmsg;
3383                         }
3384                 } else {
3385                         PBD::warning << "Automation not available for " << path << endmsg;
3386                 }
3387
3388                 if (control) {
3389
3390                         switch (aut) {
3391                                 case 0:
3392                                         control->set_automation_state (ARDOUR::Off);
3393                                         ret = 0;
3394                                         break;
3395                                 case 1:
3396                                         control->set_automation_state (ARDOUR::Play);
3397                                         ret = 0;
3398                                         break;
3399                                 case 2:
3400                                         control->set_automation_state (ARDOUR::Write);
3401                                         ret = 0;
3402                                         break;
3403                                 case 3:
3404                                         control->set_automation_state (ARDOUR::Touch);
3405                                         ret = 0;
3406                                         break;
3407                                 default:
3408                                         break;
3409                         }
3410                 }
3411         }
3412
3413         return ret;
3414 }
3415
3416 int
3417 OSC::touch_detect (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
3418 {
3419         if (!session) return -1;
3420
3421         int ret = 1;
3422         OSCSurface *sur = get_surface(get_address (msg));
3423         boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
3424         uint32_t ctr = 0;
3425         uint32_t touch = 0;
3426         uint32_t ssid;
3427
3428         if (argc) {
3429                 if (types[argc - 1] == 'f') {
3430                         touch = (int)argv[argc - 1]->f;
3431                 } else {
3432                         touch = argv[argc - 1]->i;
3433                 }
3434         }
3435
3436         //parse path first to find stripable
3437         if (!strncmp (path, X_("/strip/"), 7)) {
3438                 // find ssid and stripable
3439                 if (argc > 1) {
3440                         if (types[0] == 'f') {
3441                                 ssid = (uint32_t)argv[0]->f;
3442                         } else {
3443                                 ssid = argv[0]->i;
3444                         }
3445                         strp = get_strip (ssid, get_address (msg));
3446                 } else {
3447                         ssid = atoi (&(strrchr (path, '/' ))[1]);
3448                         strp = get_strip (ssid, get_address (msg));
3449                 }
3450                 ctr = 7;
3451         } else if (!strncmp (path, X_("/select/"), 8)) {
3452                 if (sur->expand_enable && sur->expand) {
3453                         strp = get_strip (sur->expand, get_address (msg));
3454                 } else {
3455                         strp = _select;
3456                 }
3457                 ctr = 8;
3458         } else {
3459                 return ret;
3460         }
3461         if (strp) {
3462                 boost::shared_ptr<AutomationControl> control = boost::shared_ptr<AutomationControl>();
3463                 // other automatable controls can be added by repeating the next 6.5 lines
3464                 if ((!strncmp (&path[ctr], X_("fader"), 5)) || (!strncmp (&path[ctr], X_("gain"), 4))) {
3465                         if (strp->gain_control ()) {
3466                                 control = strp->gain_control ();
3467                         } else {
3468                                 PBD::warning << "No fader for this strip" << endmsg;
3469                         }
3470                 } else {
3471                         PBD::warning << "Automation not available for " << path << endmsg;
3472                 }
3473
3474                 if (control) {
3475                         if (touch) {
3476                                 //start touch
3477                                 control->start_touch (control->session().transport_sample());
3478                                 ret = 0;
3479                         } else {
3480                                 // end touch
3481                                 control->stop_touch (control->session().transport_sample());
3482                                 ret = 0;
3483                         }
3484                         // just in case some crazy surface starts sending control values before touch
3485                         FakeTouchMap::iterator x = _touch_timeout.find(control);
3486                         if (x != _touch_timeout.end()) {
3487                                 _touch_timeout.erase (x);
3488                         }
3489                 }
3490         }
3491
3492         return ret;
3493 }
3494
3495 int
3496 OSC::fake_touch (boost::shared_ptr<ARDOUR::AutomationControl> ctrl)
3497 {
3498         if (ctrl) {
3499                 //start touch
3500                 if (ctrl->automation_state() == Touch && !ctrl->touching ()) {
3501                 ctrl->start_touch (ctrl->session().transport_sample());
3502                 _touch_timeout[ctrl] = 10;
3503                 }
3504         }
3505
3506         return 0;
3507 }
3508
3509 int
3510 OSC::route_mute (int ssid, int yn, lo_message msg)
3511 {
3512         if (!session) return -1;
3513         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3514         OSCSurface *sur = get_surface(get_address (msg));
3515
3516         if (s) {
3517                 if (s->mute_control()) {
3518                         s->mute_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3519                         return 0;
3520                 }
3521         }
3522
3523         return float_message_with_id (X_("/strip/mute"), ssid, 0, sur->feedback[2], get_address (msg));
3524 }
3525
3526 int
3527 OSC::sel_mute (uint32_t yn, lo_message msg)
3528 {
3529         OSCSurface *sur = get_surface(get_address (msg));
3530         boost::shared_ptr<Stripable> s;
3531         if (sur->expand_enable) {
3532                 s = get_strip (sur->expand, get_address (msg));
3533         } else {
3534                 s = _select;
3535         }
3536         if (s) {
3537                 if (s->mute_control()) {
3538                         s->mute_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3539                         return 0;
3540                 }
3541         }
3542         return float_message(X_("/select/mute"), 0, get_address (msg));
3543 }
3544
3545 int
3546 OSC::route_solo (int ssid, int yn, lo_message msg)
3547 {
3548         if (!session) return -1;
3549         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3550         OSCSurface *sur = get_surface(get_address (msg));
3551
3552         if (s) {
3553                 if (s->solo_control()) {
3554                         s->solo_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3555                 }
3556         }
3557
3558         return float_message_with_id (X_("/strip/solo"), ssid, 0, sur->feedback[2], get_address (msg));
3559 }
3560
3561 int
3562 OSC::route_solo_iso (int ssid, int yn, lo_message msg)
3563 {
3564         if (!session) return -1;
3565         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3566         OSCSurface *sur = get_surface(get_address (msg));
3567
3568         if (s) {
3569                 if (s->solo_isolate_control()) {
3570                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3571                         return 0;
3572                 }
3573         }
3574
3575         return float_message_with_id (X_("/strip/solo_iso"), ssid, 0, sur->feedback[2], get_address (msg));
3576 }
3577
3578 int
3579 OSC::route_solo_safe (int ssid, int yn, lo_message msg)
3580 {
3581         if (!session) return -1;
3582         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
3583         OSCSurface *sur = get_surface(get_address (msg));
3584
3585         if (s) {
3586                 if (s->solo_safe_control()) {
3587                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3588                         return 0;
3589                 }
3590         }
3591
3592         return float_message_with_id (X_("/strip/solo_safe"), ssid, 0, sur->feedback[2], get_address (msg));
3593 }
3594
3595 int
3596 OSC::sel_solo (uint32_t yn, lo_message msg)
3597 {
3598         OSCSurface *sur = get_surface(get_address (msg));
3599         boost::shared_ptr<Stripable> s;
3600         if (sur->expand_enable) {
3601                 s = get_strip (sur->expand, get_address (msg));
3602         } else {
3603                 s = _select;
3604         }
3605         if (s) {
3606                 if (s->solo_control()) {
3607                         session->set_control (s->solo_control(), yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3608                 }
3609         }
3610         return float_message(X_("/select/solo"), 0, get_address (msg));
3611 }
3612
3613 int
3614 OSC::sel_solo_iso (uint32_t yn, 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->solo_isolate_control()) {
3625                         s->solo_isolate_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3626                         return 0;
3627                 }
3628         }
3629         return float_message(X_("/select/solo_iso"), 0, get_address (msg));
3630 }
3631
3632 int
3633 OSC::sel_solo_safe (uint32_t yn, 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->solo_safe_control()) {
3644                         s->solo_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3645                         return 0;
3646                 }
3647         }
3648         return float_message(X_("/select/solo_safe"), 0, get_address (msg));
3649 }
3650
3651 int
3652 OSC::sel_recenable (uint32_t yn, 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->rec_enable_control()) {
3663                         s->rec_enable_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3664                         if (s->rec_enable_control()->get_value()) {
3665                                 return 0;
3666                         }
3667                 }
3668         }
3669         return float_message(X_("/select/recenable"), 0, get_address (msg));
3670 }
3671
3672 int
3673 OSC::route_recenable (int ssid, int yn, lo_message msg)
3674 {
3675         if (!session) return -1;
3676         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3677         OSCSurface *sur = get_surface(get_address (msg));
3678
3679         if (s) {
3680                 if (s->rec_enable_control()) {
3681                         s->rec_enable_control()->set_value (yn, sur->usegroup);
3682                         if (s->rec_enable_control()->get_value()) {
3683                                 return 0;
3684                         }
3685                 }
3686         }
3687         return float_message_with_id (X_("/strip/recenable"), ssid, 0, sur->feedback[2], get_address (msg));
3688 }
3689
3690 int
3691 OSC::route_rename(int ssid, char *newname, lo_message msg) {
3692     if (!session) {
3693         return -1;
3694     }
3695
3696     boost::shared_ptr<Stripable> s = get_strip(ssid, get_address(msg));
3697
3698     if (s) {
3699         s->set_name(std::string(newname));
3700     }
3701
3702     return 0;
3703 }
3704
3705 int
3706 OSC::sel_recsafe (uint32_t yn, lo_message msg)
3707 {
3708         OSCSurface *sur = get_surface(get_address (msg));
3709         boost::shared_ptr<Stripable> s;
3710         if (sur->expand_enable) {
3711                 s = get_strip (sur->expand, get_address (msg));
3712         } else {
3713                 s = _select;
3714         }
3715         if (s) {
3716                 if (s->rec_safe_control()) {
3717                         s->rec_safe_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3718                         if (s->rec_safe_control()->get_value()) {
3719                                 return 0;
3720                         }
3721                 }
3722         }
3723         return float_message(X_("/select/record_safe"), 0, get_address (msg));
3724 }
3725
3726 int
3727 OSC::route_recsafe (int ssid, int yn, lo_message msg)
3728 {
3729         if (!session) return -1;
3730         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3731         OSCSurface *sur = get_surface(get_address (msg));
3732         if (s) {
3733                 if (s->rec_safe_control()) {
3734                         s->rec_safe_control()->set_value (yn, sur->usegroup);
3735                         if (s->rec_safe_control()->get_value()) {
3736                                 return 0;
3737                         }
3738                 }
3739         }
3740         return float_message_with_id (X_("/strip/record_safe"), ssid, 0, sur->feedback[2], get_address (msg));
3741 }
3742
3743 int
3744 OSC::route_monitor_input (int ssid, int yn, lo_message msg)
3745 {
3746         if (!session) return -1;
3747         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3748         OSCSurface *sur = get_surface(get_address (msg));
3749
3750         if (s) {
3751                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3752                 if (track) {
3753                         if (track->monitoring_control()) {
3754                                 std::bitset<32> value = track->monitoring_control()->get_value ();
3755                                 value[0] = yn ? 1 : 0;
3756                                 track->monitoring_control()->set_value (value.to_ulong(), sur->usegroup);
3757                                 return 0;
3758                         }
3759                 }
3760         }
3761
3762         return float_message_with_id (X_("/strip/monitor_input"), ssid, 0, sur->feedback[2], get_address (msg));
3763 }
3764
3765 int
3766 OSC::sel_monitor_input (uint32_t yn, lo_message msg)
3767 {
3768         OSCSurface *sur = get_surface(get_address (msg));
3769         boost::shared_ptr<Stripable> s;
3770         if (sur->expand_enable) {
3771                 s = get_strip (sur->expand, get_address (msg));
3772         } else {
3773                 s = _select;
3774         }
3775         if (s) {
3776                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3777                 if (track) {
3778                         if (track->monitoring_control()) {
3779                                 std::bitset<32> value = track->monitoring_control()->get_value ();
3780                                 value[0] = yn ? 1 : 0;
3781                                 track->monitoring_control()->set_value (value.to_ulong(), sur->usegroup);
3782                                 return 0;
3783                         }
3784                 }
3785         }
3786         return float_message(X_("/select/monitor_input"), 0, get_address (msg));
3787 }
3788
3789 int
3790 OSC::route_monitor_disk (int ssid, int yn, lo_message msg)
3791 {
3792         if (!session) return -1;
3793         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3794         OSCSurface *sur = get_surface(get_address (msg));
3795
3796         if (s) {
3797                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3798                 if (track) {
3799                         if (track->monitoring_control()) {
3800                                 std::bitset<32> value = track->monitoring_control()->get_value ();
3801                                 value[1] = yn ? 1 : 0;
3802                                 track->monitoring_control()->set_value (value.to_ulong(), sur->usegroup);
3803                                 return 0;
3804                         }
3805                 }
3806         }
3807
3808         return float_message_with_id (X_("/strip/monitor_disk"), ssid, 0, sur->feedback[2], get_address (msg));
3809 }
3810
3811 int
3812 OSC::sel_monitor_disk (uint32_t yn, lo_message msg)
3813 {
3814         OSCSurface *sur = get_surface(get_address (msg));
3815         boost::shared_ptr<Stripable> s;
3816         if (sur->expand_enable) {
3817                 s = get_strip (sur->expand, get_address (msg));
3818         } else {
3819                 s = _select;
3820         }
3821         if (s) {
3822                 boost::shared_ptr<Track> track = boost::dynamic_pointer_cast<Track> (s);
3823                 if (track) {
3824                         if (track->monitoring_control()) {
3825                                 std::bitset<32> value = track->monitoring_control()->get_value ();
3826                                 value[1] = yn ? 1 : 0;
3827                                 track->monitoring_control()->set_value (value.to_ulong(), sur->usegroup);
3828                                 return 0;
3829                         }
3830                 }
3831         }
3832         return float_message(X_("/select/monitor_disk"), 0, get_address (msg));
3833 }
3834
3835
3836 int
3837 OSC::strip_phase (int ssid, int yn, lo_message msg)
3838 {
3839         if (!session) return -1;
3840         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3841         OSCSurface *sur = get_surface(get_address (msg));
3842
3843         if (s) {
3844                 if (s->phase_control()) {
3845                         s->phase_control()->set_value (yn ? 1.0 : 0.0, sur->usegroup);
3846                         return 0;
3847                 }
3848         }
3849
3850         return float_message_with_id (X_("/strip/polarity"), ssid, 0, sur->feedback[2], get_address (msg));
3851 }
3852
3853 int
3854 OSC::sel_phase (uint32_t yn, lo_message msg)
3855 {
3856         OSCSurface *sur = get_surface(get_address (msg));
3857         boost::shared_ptr<Stripable> s;
3858         if (sur->expand_enable) {
3859                 s = get_strip (sur->expand, get_address (msg));
3860         } else {
3861                 s = _select;
3862         }
3863         if (s) {
3864                 if (s->phase_control()) {
3865                         s->phase_control()->set_value (yn ? 1.0 : 0.0, PBD::Controllable::NoGroup);
3866                         return 0;
3867                 }
3868         }
3869         return float_message(X_("/select/polarity"), 0, get_address (msg));
3870 }
3871
3872 int
3873 OSC::strip_expand (int ssid, int yn, lo_message msg)
3874 {
3875         OSCSurface *sur = get_surface(get_address (msg));
3876         sur->expand_enable = (bool) yn;
3877         sur->expand = ssid;
3878         boost::shared_ptr<Stripable> s;
3879         if (yn) {
3880                 s = get_strip (ssid, get_address (msg));
3881         } else {
3882                 s = _select;
3883         }
3884
3885         return _strip_select (s, get_address (msg));
3886 }
3887
3888 int
3889 OSC::strip_hide (int ssid, int state, lo_message msg)
3890 {
3891         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3892
3893         if (s) {
3894                 if (state != s->is_hidden ()) {
3895                         s->presentation_info().set_hidden ((bool) state);
3896                 }
3897         }
3898         return 0;
3899 }
3900
3901 int
3902 OSC::_strip_select (boost::shared_ptr<Stripable> s, lo_address addr)
3903 {
3904         if (!session) {
3905                 return -1;
3906         }
3907         OSCSurface *sur = get_surface(addr, true);
3908         if (!s) {
3909                 // expand doesn't point to a stripable, turn it off and use select
3910                 sur->expand = 0;
3911                 sur->expand_enable = false;
3912                 if (ControlProtocol::first_selected_stripable()) {
3913                         s = ControlProtocol::first_selected_stripable();
3914                 } else {
3915                         s = session->master_out ();
3916                 }
3917                         _select = s;
3918         }
3919         sur->select = s;
3920         bool sends;
3921         uint32_t nsends  = 0;
3922         do {
3923                 sends = false;
3924                 if (s->send_level_controllable (nsends)) {
3925                         sends = true;
3926                         nsends++;
3927                 }
3928         } while (sends);
3929         sur->nsends = nsends;
3930
3931         s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
3932
3933         OSCSelectObserver* so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs);
3934         if (sur->feedback[13]) {
3935                 if (so != 0) {
3936                         so->refresh_strip (s, nsends, sur->gainmode, true);
3937                 } else {
3938                         OSCSelectObserver* sel_fb = new OSCSelectObserver (*this, sur);
3939                         sur->sel_obs = sel_fb;
3940                 }
3941                 sur->sel_obs->set_expand (sur->expand_enable);
3942                 uint32_t obs_expand = 0;
3943                 if (sur->expand_enable) {
3944                         obs_expand = sur->expand;
3945                 } else {
3946                         obs_expand = 0;
3947                 }
3948                 for (uint32_t i = 0; i < sur->observers.size(); i++) {
3949                         sur->observers[i]->set_expand (obs_expand);
3950                 }
3951         } else {
3952                 if (so != 0) {
3953                         delete so;
3954                         sur->sel_obs = 0;
3955                 }
3956         }
3957         // need to set monitor for processor changed signal (for paging)
3958         string address = lo_address_get_url (addr);
3959         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
3960         if (r) {
3961                 r->processors_changed.connect  (sur->proc_connection, MISSING_INVALIDATOR, boost::bind (&OSC::processor_changed, this, address), this);
3962                 _sel_plugin (sur->plugin_id, addr);
3963         }
3964
3965         return 0;
3966 }
3967
3968 void
3969 OSC::processor_changed (string address)
3970 {
3971         lo_address addr = lo_address_new_from_url (address.c_str());
3972         OSCSurface *sur = get_surface (addr);
3973         _sel_plugin (sur->plugin_id, addr);
3974         if (sur->sel_obs) {
3975                 sur->sel_obs->renew_sends ();
3976                 sur->sel_obs->eq_restart (-1);
3977         }
3978 }
3979
3980 int
3981 OSC::strip_gui_select (int ssid, int yn, lo_message msg)
3982 {
3983         //ignore button release
3984         if (!yn) return 0;
3985
3986         if (!session) {
3987                 return -1;
3988         }
3989         OSCSurface *sur = get_surface(get_address (msg));
3990         sur->expand_enable = false;
3991         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
3992         if (s) {
3993                 SetStripableSelection (s);
3994         } else {
3995                 if ((int) (sur->feedback.to_ulong())) {
3996                         float_message_with_id (X_("/strip/select"), ssid, 0, sur->feedback[2], get_address (msg));
3997                 }
3998         }
3999
4000         return 0;
4001 }
4002
4003 int
4004 OSC::sel_expand (uint32_t state, lo_message msg)
4005 {
4006         OSCSurface *sur = get_surface(get_address (msg));
4007         boost::shared_ptr<Stripable> s;
4008         if (state && sur->expand) {
4009                 sur->expand_enable = (bool) state;
4010                 s = get_strip (sur->expand, get_address (msg));
4011         } else {
4012                 sur->expand_enable = false;
4013                 s = _select;
4014         }
4015
4016         return _strip_select (s, get_address (msg));
4017 }
4018
4019 int
4020 OSC::route_set_gain_dB (int ssid, float dB, lo_message msg)
4021 {
4022         if (!session) {
4023                 return -1;
4024         }
4025         OSCSurface *sur = get_surface(get_address (msg));
4026         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4027         if (s) {
4028                 float abs;
4029                 if (s->gain_control()) {
4030                         if (dB < -192) {
4031                                 abs = 0;
4032                         } else {
4033                                 abs = dB_to_coefficient (dB);
4034                                 float top = s->gain_control()->upper();
4035                                 if (abs > top) {
4036                                         abs = top;
4037                                 }
4038                         }
4039                         fake_touch (s->gain_control());
4040                         s->gain_control()->set_value (abs, sur->usegroup);
4041                         return 0;
4042                 }
4043         }
4044         return float_message_with_id (X_("/strip/gain"), ssid, -193, sur->feedback[2], get_address (msg));
4045 }
4046
4047 int
4048 OSC::sel_gain (float val, lo_message msg)
4049 {
4050         OSCSurface *sur = get_surface(get_address (msg));
4051         boost::shared_ptr<Stripable> s;
4052         if (sur->expand_enable) {
4053                 s = get_strip (sur->expand, get_address (msg));
4054         } else {
4055                 s = _select;
4056         }
4057         if (s) {
4058                 float abs;
4059                 if (s->gain_control()) {
4060                         if (val < -192) {
4061                                 abs = 0;
4062                         } else {
4063                                 abs = dB_to_coefficient (val);
4064                                 float top = s->gain_control()->upper();
4065                                 if (abs > top) {
4066                                         abs = top;
4067                                 }
4068                         }
4069                         fake_touch (s->gain_control());
4070                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4071                         return 0;
4072                 }
4073         }
4074         return float_message(X_("/select/gain"), -193, get_address (msg));
4075 }
4076
4077 int
4078 OSC::sel_dB_delta (float delta, lo_message msg)
4079 {
4080         OSCSurface *sur = get_surface(get_address (msg));
4081         boost::shared_ptr<Stripable> s;
4082         if (sur->expand_enable) {
4083                 s = get_strip (sur->expand, get_address (msg));
4084         } else {
4085                 s = _select;
4086         }
4087         if (s) {
4088                 if (s->gain_control()) {
4089                         float dB = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
4090                         float abs;
4091                         if (dB < -192) {
4092                                 abs = 0;
4093                         } else {
4094                                 abs = dB_to_coefficient (dB);
4095                                 float top = s->gain_control()->upper();
4096                                 if (abs > top) {
4097                                         abs = top;
4098                                 }
4099                         }
4100                         fake_touch (s->gain_control());
4101                         s->gain_control()->set_value (abs, PBD::Controllable::NoGroup);
4102                         return 0;
4103                 }
4104         }
4105         return float_message(X_("/select/gain"), -193, get_address (msg));
4106 }
4107
4108 int
4109 OSC::route_set_gain_fader (int ssid, float pos, lo_message msg)
4110 {
4111         if (!session) {
4112                 return -1;
4113         }
4114         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4115         OSCSurface *sur = get_surface(get_address (msg));
4116
4117         if (s) {
4118                 if (s->gain_control()) {
4119                         fake_touch (s->gain_control());
4120                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (pos), sur->usegroup);
4121                 } else {
4122                         return float_message_with_id (X_("/strip/fader"), ssid, 0, sur->feedback[2], get_address (msg));
4123                 }
4124         } else {
4125                 return float_message_with_id (X_("/strip/fader"), ssid, 0, sur->feedback[2], get_address (msg));
4126         }
4127         return 0;
4128 }
4129
4130 int
4131 OSC::strip_db_delta (int ssid, float delta, lo_message msg)
4132 {
4133         if (!session) return -1;
4134         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4135         OSCSurface *sur = get_surface(get_address (msg));
4136         if (s) {
4137                 float db = accurate_coefficient_to_dB (s->gain_control()->get_value()) + delta;
4138                 float abs;
4139                 if (db < -192) {
4140                         abs = 0;
4141                 } else {
4142                         abs = dB_to_coefficient (db);
4143                         float top = s->gain_control()->upper();
4144                         if (abs > top) {
4145                                 abs = top;
4146                         }
4147                 }
4148                 s->gain_control()->set_value (abs, sur->usegroup);
4149                 return 0;
4150         }
4151         return -1;
4152 }
4153
4154 int
4155 OSC::sel_fader (float val, lo_message msg)
4156 {
4157         OSCSurface *sur = get_surface(get_address (msg));
4158         boost::shared_ptr<Stripable> s;
4159         if (sur->expand_enable) {
4160                 s = get_strip (sur->expand, get_address (msg));
4161         } else {
4162                 s = _select;
4163         }
4164         if (s) {
4165                 if (s->gain_control()) {
4166                         fake_touch (s->gain_control());
4167                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4168                         return 0;
4169                 }
4170         }
4171         return float_message(X_("/select/fader"), 0, get_address (msg));
4172 }
4173
4174 int
4175 OSC::route_set_trim_abs (int ssid, float level, lo_message msg)
4176 {
4177         if (!session) return -1;
4178         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4179         OSCSurface *sur = get_surface(get_address (msg));
4180
4181         if (s) {
4182                 if (s->trim_control()) {
4183                         s->trim_control()->set_value (level, sur->usegroup);
4184                         return 0;
4185                 }
4186
4187         }
4188
4189         return -1;
4190 }
4191
4192 int
4193 OSC::route_set_trim_dB (int ssid, float dB, lo_message msg)
4194 {
4195         OSCSurface *sur = get_surface(get_address (msg));
4196         int ret;
4197         ret = route_set_trim_abs(ssid, dB_to_coefficient (dB), msg);
4198         if (ret != 0) {
4199                 return float_message_with_id (X_("/strip/trimdB"), ssid, 0, sur->feedback[2], get_address (msg));
4200         }
4201
4202 return 0;
4203 }
4204
4205 int
4206 OSC::sel_trim (float val, lo_message msg)
4207 {
4208         OSCSurface *sur = get_surface(get_address (msg));
4209         boost::shared_ptr<Stripable> s;
4210         if (sur->expand_enable) {
4211                 s = get_strip (sur->expand, get_address (msg));
4212         } else {
4213                 s = _select;
4214         }
4215         if (s) {
4216                 if (s->trim_control()) {
4217                         s->trim_control()->set_value (dB_to_coefficient (val), PBD::Controllable::NoGroup);
4218                         return 0;
4219                 }
4220         }
4221         return float_message(X_("/select/trimdB"), 0, get_address (msg));
4222 }
4223
4224 int
4225 OSC::sel_hide (uint32_t state, lo_message msg)
4226 {
4227         OSCSurface *sur = get_surface(get_address (msg));
4228         boost::shared_ptr<Stripable> s;
4229         if (sur->expand_enable) {
4230                 s = get_strip (sur->expand, get_address (msg));
4231         } else {
4232                 s = _select;
4233         }
4234         if (s) {
4235                 if (state != s->is_hidden ()) {
4236                         s->presentation_info().set_hidden ((bool) state);
4237                 }
4238         }
4239         return 0;
4240 }
4241
4242 int
4243 OSC::sel_pan_position (float val, lo_message msg)
4244 {
4245         OSCSurface *sur = get_surface(get_address (msg));
4246         boost::shared_ptr<Stripable> s;
4247         if (sur->expand_enable) {
4248                 s = get_strip (sur->expand, get_address (msg));
4249         } else {
4250                 s = _select;
4251         }
4252         if (s) {
4253                 if(s->pan_azimuth_control()) {
4254                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4255                         return 0;
4256                 }
4257         }
4258         return float_message(X_("/select/pan_stereo_position"), 0.5, get_address (msg));
4259 }
4260
4261 int
4262 OSC::sel_pan_width (float val, lo_message msg)
4263 {
4264         OSCSurface *sur = get_surface(get_address (msg));
4265         boost::shared_ptr<Stripable> s;
4266         if (sur->expand_enable) {
4267                 s = get_strip (sur->expand, get_address (msg));
4268         } else {
4269                 s = _select;
4270         }
4271         if (s) {
4272                 if (s->pan_width_control()) {
4273                         s->pan_width_control()->set_value (s->pan_width_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
4274                         return 0;
4275                 }
4276         }
4277         return float_message(X_("/select/pan_stereo_width"), 1, get_address (msg));
4278 }
4279
4280 int
4281 OSC::route_set_pan_stereo_position (int ssid, float pos, lo_message msg)
4282 {
4283         if (!session) return -1;
4284         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4285         OSCSurface *sur = get_surface(get_address (msg));
4286
4287         if (s) {
4288                 if(s->pan_azimuth_control()) {
4289                         s->pan_azimuth_control()->set_value (s->pan_azimuth_control()->interface_to_internal (pos), sur->usegroup);
4290                         return 0;
4291                 }
4292         }
4293
4294         return float_message_with_id (X_("/strip/pan_stereo_position"), ssid, 0.5, sur->feedback[2], get_address (msg));
4295 }
4296
4297 int
4298 OSC::route_set_pan_stereo_width (int ssid, float pos, lo_message msg)
4299 {
4300         if (!session) return -1;
4301         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4302         OSCSurface *sur = get_surface(get_address (msg));
4303
4304         if (s) {
4305                 if (s->pan_width_control()) {
4306                         s->pan_width_control()->set_value (pos, sur->usegroup);
4307                         return 0;
4308                 }
4309         }
4310
4311         return float_message_with_id (X_("/strip/pan_stereo_width"), ssid, 1, sur->feedback[2], get_address (msg));
4312 }
4313
4314 int
4315 OSC::route_set_send_gain_dB (int ssid, int id, float val, lo_message msg)
4316 {
4317         if (!session) {
4318                 return -1;
4319         }
4320         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4321         OSCSurface *sur = get_surface(get_address (msg));
4322         float abs;
4323         if (s) {
4324                 if (id > 0) {
4325                         --id;
4326                 }
4327 #ifdef MIXBUS
4328                 abs = val;
4329 #else
4330                 if (val < -192) {
4331                         abs = 0;
4332                 } else {
4333                         abs = dB_to_coefficient (val);
4334                 }
4335 #endif
4336                 if (s->send_level_controllable (id)) {
4337                         s->send_level_controllable (id)->set_value (abs, sur->usegroup);
4338                         return 0;
4339                 }
4340         }
4341         return 0;
4342 }
4343
4344 int
4345 OSC::route_set_send_fader (int ssid, int id, float val, lo_message msg)
4346 {
4347         if (!session) {
4348                 return -1;
4349         }
4350         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4351         OSCSurface *sur = get_surface(get_address (msg));
4352         float abs;
4353         if (s) {
4354
4355                 if (id > 0) {
4356                         --id;
4357                 }
4358
4359                 if (s->send_level_controllable (id)) {
4360                         abs = s->send_level_controllable(id)->interface_to_internal (val);
4361                         s->send_level_controllable (id)->set_value (abs, sur->usegroup);
4362                         return 0;
4363                 }
4364         }
4365         return 0;
4366 }
4367
4368 int
4369 OSC::sel_sendgain (int id, float val, lo_message msg)
4370 {
4371         OSCSurface *sur = get_surface(get_address (msg));
4372         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
4373                 return float_message_with_id (X_("/select/send_gain"), id, -193, sur->feedback[2], get_address (msg));
4374         }
4375         boost::shared_ptr<Stripable> s;
4376         if (sur->expand_enable) {
4377                 s = get_strip (sur->expand, get_address (msg));
4378         } else {
4379                 s = _select;
4380         }
4381         float abs;
4382         int send_id = 0;
4383         if (s) {
4384                 if (id > 0) {
4385                         send_id = id - 1;
4386                 }
4387 #ifdef MIXBUS
4388                 abs = val;
4389 #else
4390                 if (val < -192) {
4391                         abs = 0;
4392                 } else {
4393                         abs = dB_to_coefficient (val);
4394                 }
4395 #endif
4396                 if (sur->send_page_size) {
4397                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
4398                 }
4399                 if (s->send_level_controllable (send_id)) {
4400                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
4401                         return 0;
4402                 }
4403         }
4404         return float_message_with_id (X_("/select/send_gain"), id, -193, sur->feedback[2], get_address (msg));
4405 }
4406
4407 int
4408 OSC::sel_sendfader (int id, float val, lo_message msg)
4409 {
4410         OSCSurface *sur = get_surface(get_address (msg));
4411         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
4412                 return float_message_with_id (X_("/select/send_fader"), id, 0, sur->feedback[2], get_address (msg));
4413         }
4414         boost::shared_ptr<Stripable> s;
4415         if (sur->expand_enable) {
4416                 s = get_strip (sur->expand, get_address (msg));
4417         } else {
4418                 s = _select;
4419         }
4420         float abs;
4421         int send_id = 0;
4422         if (s) {
4423
4424                 if (id > 0) {
4425                         send_id = id - 1;
4426                 }
4427                 if (sur->send_page_size) {
4428                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
4429                 }
4430
4431                 if (s->send_level_controllable (send_id)) {
4432                         abs = s->send_level_controllable(send_id)->interface_to_internal (val);
4433                         s->send_level_controllable (send_id)->set_value (abs, PBD::Controllable::NoGroup);
4434                         return 0;
4435                 }
4436         }
4437         return float_message_with_id (X_("/select/send_fader"), id, 0, sur->feedback[2], get_address (msg));
4438 }
4439
4440 int
4441 OSC::route_set_send_enable (int ssid, int sid, float val, lo_message msg)
4442 {
4443         if (!session) {
4444                 return -1;
4445         }
4446         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4447         OSCSurface *sur = get_surface(get_address (msg));
4448
4449         if (s) {
4450
4451                 /* revert to zero-based counting */
4452
4453                 if (sid > 0) {
4454                         --sid;
4455                 }
4456
4457                 if (s->send_enable_controllable (sid)) {
4458                         s->send_enable_controllable (sid)->set_value (val, sur->usegroup);
4459                         return 0;
4460                 }
4461
4462                 if (s->send_level_controllable (sid)) {
4463                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4464                         if (!r) {
4465                                 return 0;
4466                         }
4467                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(sid));
4468                         if (snd) {
4469                                 if (val) {
4470                                         snd->activate();
4471                                 } else {
4472                                         snd->deactivate();
4473                                 }
4474                         }
4475                         return 0;
4476                 }
4477
4478         }
4479
4480         return -1;
4481 }
4482
4483 int
4484 OSC::sel_sendenable (int id, float val, lo_message msg)
4485 {
4486         OSCSurface *sur = get_surface(get_address (msg));
4487         if (sur->send_page_size && (id > (int)sur->send_page_size)) {
4488                 return float_message_with_id (X_("/select/send_enable"), id, 0, sur->feedback[2], get_address (msg));
4489         }
4490         boost::shared_ptr<Stripable> s;
4491         if (sur->expand_enable) {
4492                 s = get_strip (sur->expand, get_address (msg));
4493         } else {
4494                 s = _select;
4495         }
4496         int send_id = 0;
4497         if (s) {
4498                 if (id > 0) {
4499                         send_id = id - 1;
4500                 }
4501                 if (sur->send_page_size) {
4502                         send_id = send_id + ((sur->send_page - 1) * sur->send_page_size);
4503                 }
4504                 if (s->send_enable_controllable (send_id)) {
4505                         s->send_enable_controllable (send_id)->set_value (val, PBD::Controllable::NoGroup);
4506                         return 0;
4507                 }
4508                 if (s->send_level_controllable (send_id)) {
4509                         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4510                         if (!r) {
4511                                 // should never get here
4512                                 return float_message_with_id (X_("/select/send_enable"), id, 0, sur->feedback[2], get_address (msg));
4513                         }
4514                         boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(send_id));
4515                         if (snd) {
4516                                 if (val) {
4517                                         snd->activate();
4518                                 } else {
4519                                         snd->deactivate();
4520                                 }
4521                         }
4522                         return 0;
4523                 }
4524         }
4525         return float_message_with_id (X_("/select/send_enable"), id, 0, sur->feedback[2], get_address (msg));
4526 }
4527
4528 int
4529 OSC::sel_master_send_enable (int state, lo_message msg)
4530 {
4531         OSCSurface *sur = get_surface(get_address (msg));
4532         boost::shared_ptr<Stripable> s;
4533         if (sur->expand_enable) {
4534                 s = get_strip (sur->expand, get_address (msg));
4535         } else {
4536                 s = _select;
4537         }
4538         if (s) {
4539                 if (s->master_send_enable_controllable ()) {
4540                         s->master_send_enable_controllable()->set_value (state, PBD::Controllable::NoGroup);
4541                         return 0;
4542                 }
4543         }
4544         return float_message (X_("/select/master_send_enable"), 0, get_address(msg));
4545 }
4546
4547 int
4548 OSC::select_plugin_parameter (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg) {
4549         OSCSurface *sur = get_surface(get_address (msg));
4550         int paid;
4551         uint32_t piid = sur->plugin_id;
4552         float value = 0;
4553         if (argc > 1) {
4554                 // no inline args
4555                 if (argc == 2) {
4556                         // change parameter in already selected plugin
4557                         if (types[0]  == 'f') {
4558                                 paid = (int) argv[0]->f;
4559                         } else {
4560                                 paid = argv[0]->i;
4561                         }
4562                         value = argv[1]->f;
4563                 } else if (argc == 3) {
4564                         if (types[0] == 'f') {
4565                                 piid = (int) argv[0]->f;
4566                         } else {
4567                                 piid = argv[0]->i;
4568                         }
4569                         _sel_plugin (piid, get_address (msg));
4570                         if (types[1] == 'f') {
4571                                 paid = (int) argv[1]->f;
4572                         } else {
4573                                 paid = argv[1]->i;
4574                         }
4575                         value = argv[2]->f;
4576                 } else if (argc > 3) {
4577                         PBD::warning << "OSC: Too many parameters: " << argc << endmsg;
4578                         return -1;
4579                 }
4580         } else if (argc) {
4581                 const char * par = strstr (&path[25], "/");
4582                 if (par) {
4583                         piid = atoi (&path[25]);
4584                         _sel_plugin (piid, msg);
4585                         paid = atoi (&par[1]);
4586                         value = argv[0]->f;
4587                         // we have plugin id too
4588                 } else {
4589                         // just parameter
4590                         paid = atoi (&path[25]);
4591                         value = argv[0]->f;
4592                 }
4593         } else {
4594                 PBD::warning << "OSC: Must have parameters." << endmsg;
4595                 return -1;
4596         }
4597         if (!piid || piid > sur->plugins.size ()) {
4598                 return float_message_with_id (X_("/select/plugin/parameter"), paid, 0, sur->feedback[2], get_address (msg));
4599         }
4600         if (sur->plug_page_size && (paid > (int)sur->plug_page_size)) {
4601                 return float_message_with_id (X_("/select/plugin/parameter"), paid, 0, sur->feedback[2], get_address (msg));
4602         }
4603         boost::shared_ptr<Stripable> s = sur->select;
4604         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
4605         if (!r) {
4606                 return 1;
4607         }
4608
4609         boost::shared_ptr<Processor> proc = r->nth_plugin (sur->plugins[sur->plugin_id - 1]);
4610         boost::shared_ptr<PluginInsert> pi;
4611         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(proc))) {
4612                 return 1;
4613         }
4614         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4615         // paid is paged parameter convert to absolute
4616         int parid = paid + (int)sur->plug_page - 1;
4617         if (parid > (int) sur->plug_params.size ()) {
4618                 if (sur->feedback[13]) {
4619                         float_message_with_id (X_("/select/plugin/parameter"), paid, 0, sur->feedback[2], get_address (msg));
4620                 }
4621                 return 0;
4622         }
4623
4624         bool ok = false;
4625         uint32_t controlid = pip->nth_parameter(sur->plug_params[parid - 1], ok);
4626         if (!ok) {
4627                 return 1;
4628         }
4629         ParameterDescriptor pd;
4630         pip->get_parameter_descriptor(controlid, pd);
4631         if ( pip->parameter_is_input(controlid) || pip->parameter_is_control(controlid) ) {
4632                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
4633                 if (c) {
4634                         if (pd.integer_step && pd.upper == 1) {
4635                                 if (c->get_value () && value < 1.0) {
4636                                         c->set_value (0, PBD::Controllable::NoGroup);
4637                                 } else if (!c->get_value () && value) {
4638                                         c->set_value (1, PBD::Controllable::NoGroup);
4639                                 }
4640                         } else {
4641                                 c->set_value (c->interface_to_internal (value), PBD::Controllable::NoGroup);
4642                         }
4643                         return 0;
4644                 }
4645         }
4646         return 1;
4647 }
4648
4649 int
4650 OSC::sel_plugin_activate (float state, lo_message msg)
4651 {
4652         if (!session) {
4653                 return -1;
4654         }
4655         OSCSurface *sur = get_surface(get_address (msg));
4656         if (sur->plugins.size() > 0) {
4657                 boost::shared_ptr<Stripable> s = sur->select;
4658
4659                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4660
4661                 if (r) {
4662                         boost::shared_ptr<Processor> redi=r->nth_plugin (sur->plugins[sur->plugin_id -1]);
4663                         if (redi) {
4664                                 boost::shared_ptr<PluginInsert> pi;
4665                                 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4666                                         if(state > 0) {
4667                                                 pi->activate();
4668                                         } else {
4669                                                 pi->deactivate();
4670                                         }
4671                                         return 0;
4672                                 }
4673                         }
4674                 }
4675         }
4676         float_message (X_("/select/plugin/activate"), 0, get_address (msg));
4677         PBD::warning << "OSC: Select has no Plugin." << endmsg;
4678         return 0;
4679 }
4680
4681 int
4682 OSC::route_plugin_list (int ssid, lo_message msg) {
4683         if (!session) {
4684                 return -1;
4685         }
4686
4687         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4688
4689         if (!r) {
4690                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4691                 return -1;
4692         }
4693         int piid = 0;
4694
4695         lo_message reply = lo_message_new ();
4696         lo_message_add_int32 (reply, ssid);
4697
4698
4699         for (;;) {
4700                 boost::shared_ptr<Processor> redi = r->nth_plugin(piid);
4701                 if ( !redi ) {
4702                         break;
4703                 }
4704
4705                 boost::shared_ptr<PluginInsert> pi;
4706
4707                 if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4708                         PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4709                         continue;
4710                 }
4711                 lo_message_add_int32 (reply, piid + 1);
4712
4713                 boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4714                 lo_message_add_string (reply, pip->name());
4715                 lo_message_add_int32(reply, redi->enabled() ? 1 : 0);
4716
4717                 piid++;
4718         }
4719
4720         lo_send_message (get_address (msg), X_("/strip/plugin/list"), reply);
4721         lo_message_free (reply);
4722         return 0;
4723 }
4724
4725 int
4726 OSC::route_plugin_descriptor (int ssid, int piid, lo_message msg) {
4727         if (!session) {
4728                 return -1;
4729         }
4730
4731         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4732
4733         if (!r) {
4734                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4735                 return -1;
4736         }
4737
4738         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4739
4740         if (!redi) {
4741                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4742                 return -1;
4743         }
4744
4745         boost::shared_ptr<PluginInsert> pi;
4746
4747         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4748                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4749                 return -1;
4750         }
4751
4752         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4753         bool ok = false;
4754
4755         for ( uint32_t ppi = 0; ppi < pip->parameter_count(); ppi++) {
4756
4757                 uint32_t controlid = pip->nth_parameter(ppi, ok);
4758                 if (!ok) {
4759                         continue;
4760                 }
4761                 boost::shared_ptr<AutomationControl> c = pi->automation_control(Evoral::Parameter(PluginAutomation, 0, controlid));
4762
4763                 lo_message reply = lo_message_new();
4764                 lo_message_add_int32 (reply, ssid);
4765                 lo_message_add_int32 (reply, piid);
4766
4767                 lo_message_add_int32 (reply, ppi + 1);
4768                 ParameterDescriptor pd;
4769                 pi->plugin()->get_parameter_descriptor(controlid, pd);
4770                 lo_message_add_string (reply, pd.label.c_str());
4771
4772                 // I've combined those binary descriptor parts in a bit-field to reduce lilo message elements
4773                 int flags = 0;
4774                 flags |= pd.enumeration ? 1 : 0;
4775                 flags |= pd.integer_step ? 2 : 0;
4776                 flags |= pd.logarithmic ? 4 : 0;
4777                 flags |= pd.sr_dependent ? 32 : 0;
4778                 flags |= pd.toggled ? 64 : 0;
4779                 flags |= pip->parameter_is_input(controlid) ? 0x80 : 0;
4780
4781                 std::string param_desc = pi->plugin()->describe_parameter(Evoral::Parameter(PluginAutomation, 0, controlid));
4782                 flags |= (param_desc == X_("hidden")) ? 0x100 : 0;
4783                 lo_message_add_int32 (reply, flags);
4784
4785                 switch(pd.datatype) {
4786                         case ARDOUR::Variant::BEATS:
4787                                 lo_message_add_string(reply, _("BEATS"));
4788                                 break;
4789                         case ARDOUR::Variant::BOOL:
4790                                 lo_message_add_string(reply, _("BOOL"));
4791                                 break;
4792                         case ARDOUR::Variant::DOUBLE:
4793                                 lo_message_add_string(reply, _("DOUBLE"));
4794                                 break;
4795                         case ARDOUR::Variant::FLOAT:
4796                                 lo_message_add_string(reply, _("FLOAT"));
4797                                 break;
4798                         case ARDOUR::Variant::INT:
4799                                 lo_message_add_string(reply, _("INT"));
4800                                 break;
4801                         case ARDOUR::Variant::LONG:
4802                                 lo_message_add_string(reply, _("LONG"));
4803                                 break;
4804                         case ARDOUR::Variant::NOTHING:
4805                                 lo_message_add_string(reply, _("NOTHING"));
4806                                 break;
4807                         case ARDOUR::Variant::PATH:
4808                                 lo_message_add_string(reply, _("PATH"));
4809                                 break;
4810                         case ARDOUR::Variant::STRING:
4811                                 lo_message_add_string(reply, _("STRING"));
4812                                 break;
4813                         case ARDOUR::Variant::URI:
4814                                 lo_message_add_string(reply, _("URI"));
4815                                 break;
4816                         default:
4817                                 lo_message_add_string(reply, _("UNKNOWN"));
4818                                 break;
4819                 }
4820                 lo_message_add_float (reply, pd.lower);
4821                 lo_message_add_float (reply, pd.upper);
4822                 lo_message_add_string (reply, pd.print_fmt.c_str());
4823                 if ( pd.scale_points ) {
4824                         lo_message_add_int32 (reply, pd.scale_points->size());
4825                         for ( ARDOUR::ScalePoints::const_iterator i = pd.scale_points->begin(); i != pd.scale_points->end(); ++i) {
4826                                 lo_message_add_float (reply, i->second);
4827                                 lo_message_add_string (reply, ((std::string)i->first).c_str());
4828                         }
4829                 }
4830                 else {
4831                         lo_message_add_int32 (reply, 0);
4832                 }
4833                 if ( c ) {
4834                         lo_message_add_double (reply, c->get_value());
4835                 }
4836                 else {
4837                         lo_message_add_double (reply, 0);
4838                 }
4839
4840                 lo_send_message (get_address (msg), X_("/strip/plugin/descriptor"), reply);
4841                 lo_message_free (reply);
4842         }
4843
4844         lo_message reply = lo_message_new ();
4845         lo_message_add_int32 (reply, ssid);
4846         lo_message_add_int32 (reply, piid);
4847         lo_send_message (get_address (msg), X_("/strip/plugin/descriptor_end"), reply);
4848         lo_message_free (reply);
4849
4850         return 0;
4851 }
4852
4853 int
4854 OSC::route_plugin_reset (int ssid, int piid, lo_message msg) {
4855         if (!session) {
4856                 return -1;
4857         }
4858
4859         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(get_strip (ssid, get_address (msg)));
4860
4861         if (!r) {
4862                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4863                 return -1;
4864         }
4865
4866         boost::shared_ptr<Processor> redi = r->nth_plugin(piid - 1);
4867
4868         if (!redi) {
4869                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4870                 return -1;
4871         }
4872
4873         boost::shared_ptr<PluginInsert> pi;
4874
4875         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4876                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4877                 return -1;
4878         }
4879
4880         pi->reset_parameters_to_default ();
4881
4882         return 0;
4883 }
4884
4885 int
4886 OSC::route_plugin_parameter (int ssid, int piid, int par, float val, lo_message msg)
4887 {
4888         if (!session)
4889                 return -1;
4890         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4891
4892         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4893
4894         if (!r) {
4895                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
4896                 return -1;
4897         }
4898
4899         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4900
4901         if (!redi) {
4902                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
4903                 return -1;
4904         }
4905
4906         boost::shared_ptr<PluginInsert> pi;
4907
4908         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4909                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
4910                 return -1;
4911         }
4912
4913         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4914         bool ok=false;
4915
4916         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4917
4918         if (!ok) {
4919                 PBD::error << "OSC: Cannot find parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "'" << endmsg;
4920                 return -1;
4921         }
4922
4923         if (!pip->parameter_is_input(controlid)) {
4924                 PBD::error << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is not a control input" << endmsg;
4925                 return -1;
4926         }
4927
4928         ParameterDescriptor pd;
4929         pi->plugin()->get_parameter_descriptor (controlid,pd);
4930
4931         if (val >= pd.lower && val <= pd.upper) {
4932
4933                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4934                 // cerr << "parameter:" << redi->describe_parameter(controlid) << " val:" << val << "\n";
4935                 c->set_value (val, PBD::Controllable::NoGroup);
4936         } else {
4937                 PBD::warning << "OSC: Parameter # " << par <<  " for plugin # " << piid << " on RID '" << ssid << "' is out of range" << endmsg;
4938                 PBD::info << "OSC: Valid range min=" << pd.lower << " max=" << pd.upper << endmsg;
4939         }
4940
4941         return 0;
4942 }
4943
4944 //prints to cerr only
4945 int
4946 OSC::route_plugin_parameter_print (int ssid, int piid, int par, lo_message msg)
4947 {
4948         if (!session) {
4949                 return -1;
4950         }
4951         boost::shared_ptr<Stripable> s = get_strip (ssid, get_address (msg));
4952
4953         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
4954
4955         if (!r) {
4956                 return -1;
4957         }
4958
4959         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
4960
4961         if (!redi) {
4962                 return -1;
4963         }
4964
4965         boost::shared_ptr<PluginInsert> pi;
4966
4967         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
4968                 return -1;
4969         }
4970
4971         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
4972         bool ok=false;
4973
4974         uint32_t controlid = pip->nth_parameter (par - 1,ok);
4975
4976         if (!ok) {
4977                 return -1;
4978         }
4979
4980         ParameterDescriptor pd;
4981
4982         if (pi->plugin()->get_parameter_descriptor (controlid, pd) == 0) {
4983                 boost::shared_ptr<AutomationControl> c = pi->automation_control (Evoral::Parameter(PluginAutomation, 0, controlid));
4984
4985                 cerr << "parameter:     " << pd.label  << "\n";
4986                 if (c) {
4987                         cerr << "current value: " << c->get_value () << "\n";
4988                 } else {
4989                         cerr << "current value not available, control does not exist\n";
4990                 }
4991                 cerr << "lower value:   " << pd.lower << "\n";
4992                 cerr << "upper value:   " << pd.upper << "\n";
4993         }
4994
4995         return 0;
4996 }
4997
4998 int
4999 OSC::route_plugin_activate (int ssid, int piid, lo_message msg)
5000 {
5001         if (!session)
5002                 return -1;
5003         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
5004
5005         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5006
5007         if (!r) {
5008                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
5009                 return -1;
5010         }
5011
5012         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
5013
5014         if (!redi) {
5015                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
5016                 return -1;
5017         }
5018
5019         boost::shared_ptr<PluginInsert> pi;
5020
5021         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
5022                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
5023                 return -1;
5024         }
5025
5026         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
5027         pi->activate();
5028
5029         return 0;
5030 }
5031
5032 int
5033 OSC::route_plugin_deactivate (int ssid, int piid, lo_message msg)
5034 {
5035         if (!session)
5036                 return -1;
5037         boost::shared_ptr<Stripable> s = get_strip (ssid, lo_message_get_source (msg));
5038
5039         boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
5040
5041         if (!r) {
5042                 PBD::error << "OSC: Invalid Remote Control ID '" << ssid << "'" << endmsg;
5043                 return -1;
5044         }
5045
5046         boost::shared_ptr<Processor> redi=r->nth_plugin (piid - 1);
5047
5048         if (!redi) {
5049                 PBD::error << "OSC: cannot find plugin # " << piid << " for RID '" << ssid << "'" << endmsg;
5050                 return -1;
5051         }
5052
5053         boost::shared_ptr<PluginInsert> pi;
5054
5055         if (!(pi = boost::dynamic_pointer_cast<PluginInsert>(redi))) {
5056                 PBD::error << "OSC: given processor # " << piid << " on RID '" << ssid << "' is not a Plugin." << endmsg;
5057                 return -1;
5058         }
5059
5060         boost::shared_ptr<ARDOUR::Plugin> pip = pi->plugin();
5061         pi->deactivate();
5062
5063         return 0;
5064 }
5065
5066 // select
5067
5068 int
5069 OSC::sel_pan_elevation (float val, lo_message msg)
5070 {
5071         OSCSurface *sur = get_surface(get_address (msg));
5072         boost::shared_ptr<Stripable> s;
5073         if (sur->expand_enable) {
5074                 s = get_strip (sur->expand, get_address (msg));
5075         } else {
5076                 s = _select;
5077         }
5078         if (s) {
5079                 if (s->pan_elevation_control()) {
5080                         s->pan_elevation_control()->set_value (s->pan_elevation_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
5081                         return 0;
5082                 }
5083         }
5084         return float_message(X_("/select/pan_elevation_position"), 0, get_address (msg));
5085 }
5086
5087 int
5088 OSC::sel_pan_frontback (float val, lo_message msg)
5089 {
5090         OSCSurface *sur = get_surface(get_address (msg));
5091         boost::shared_ptr<Stripable> s;
5092         if (sur->expand_enable) {
5093                 s = get_strip (sur->expand, get_address (msg));
5094         } else {
5095                 s = _select;
5096         }
5097         if (s) {
5098                 if (s->pan_frontback_control()) {
5099                         s->pan_frontback_control()->set_value (s->pan_frontback_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
5100                         return 0;
5101                 }
5102         }
5103         return float_message(X_("/select/pan_frontback_position"), 0.5, get_address (msg));
5104 }
5105
5106 int
5107 OSC::sel_pan_lfe (float val, lo_message msg)
5108 {
5109         OSCSurface *sur = get_surface(get_address (msg));
5110         boost::shared_ptr<Stripable> s;
5111         if (sur->expand_enable) {
5112                 s = get_strip (sur->expand, get_address (msg));
5113         } else {
5114                 s = _select;
5115         }
5116         if (s) {
5117                 if (s->pan_lfe_control()) {
5118                         s->pan_lfe_control()->set_value (s->pan_lfe_control()->interface_to_internal (val), PBD::Controllable::NoGroup);
5119                         return 0;
5120                 }
5121         }
5122         return float_message(X_("/select/pan_lfe_control"), 0, get_address (msg));
5123 }
5124
5125 // compressor control
5126 int
5127 OSC::sel_comp_enable (float val, lo_message msg)
5128 {
5129         OSCSurface *sur = get_surface(get_address (msg));
5130         boost::shared_ptr<Stripable> s;
5131         if (sur->expand_enable) {
5132                 s = get_strip (sur->expand, get_address (msg));
5133         } else {
5134                 s = _select;
5135         }
5136         if (s) {
5137                 if (s->comp_enable_controllable()) {
5138                         s->comp_enable_controllable()->set_value (s->comp_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5139                         return 0;
5140                 }
5141         }
5142         return float_message(X_("/select/comp_enable"), 0, get_address (msg));
5143 }
5144
5145 int
5146 OSC::sel_comp_threshold (float val, lo_message msg)
5147 {
5148         OSCSurface *sur = get_surface(get_address (msg));
5149         boost::shared_ptr<Stripable> s;
5150         if (sur->expand_enable) {
5151                 s = get_strip (sur->expand, get_address (msg));
5152         } else {
5153                 s = _select;
5154         }
5155         if (s) {
5156                 if (s->comp_threshold_controllable()) {
5157                         s->comp_threshold_controllable()->set_value (s->comp_threshold_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5158                         return 0;
5159                 }
5160         }
5161         return float_message(X_("/select/comp_threshold"), 0, get_address (msg));
5162 }
5163
5164 int
5165 OSC::sel_comp_speed (float val, lo_message msg)
5166 {
5167         OSCSurface *sur = get_surface(get_address (msg));
5168         boost::shared_ptr<Stripable> s;
5169         if (sur->expand_enable) {
5170                 s = get_strip (sur->expand, get_address (msg));
5171         } else {
5172                 s = _select;
5173         }
5174         if (s) {
5175                 if (s->comp_speed_controllable()) {
5176                         s->comp_speed_controllable()->set_value (s->comp_speed_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5177                         return 0;
5178                 }
5179         }
5180         return float_message(X_("/select/comp_speed"), 0, get_address (msg));
5181 }
5182
5183 int
5184 OSC::sel_comp_mode (float val, lo_message msg)
5185 {
5186         OSCSurface *sur = get_surface(get_address (msg));
5187         boost::shared_ptr<Stripable> s;
5188         if (sur->expand_enable) {
5189                 s = get_strip (sur->expand, get_address (msg));
5190         } else {
5191                 s = _select;
5192         }
5193         if (s) {
5194                 if (s->comp_mode_controllable()) {
5195                         s->comp_mode_controllable()->set_value (s->comp_mode_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5196                         return 0;
5197                 }
5198         }
5199         return float_message(X_("/select/comp_mode"), 0, get_address (msg));
5200 }
5201
5202 int
5203 OSC::sel_comp_makeup (float val, lo_message msg)
5204 {
5205         OSCSurface *sur = get_surface(get_address (msg));
5206         boost::shared_ptr<Stripable> s;
5207         if (sur->expand_enable) {
5208                 s = get_strip (sur->expand, get_address (msg));
5209         } else {
5210                 s = _select;
5211         }
5212         if (s) {
5213                 if (s->comp_makeup_controllable()) {
5214                         s->comp_makeup_controllable()->set_value (s->comp_makeup_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5215                         return 0;
5216                 }
5217         }
5218         return float_message(X_("/select/comp_makeup"), 0, get_address (msg));
5219 }
5220
5221 // EQ control
5222
5223 int
5224 OSC::sel_eq_enable (float val, lo_message msg)
5225 {
5226         OSCSurface *sur = get_surface(get_address (msg));
5227         boost::shared_ptr<Stripable> s;
5228         if (sur->expand_enable) {
5229                 s = get_strip (sur->expand, get_address (msg));
5230         } else {
5231                 s = _select;
5232         }
5233         if (s) {
5234                 if (s->eq_enable_controllable()) {
5235                         s->eq_enable_controllable()->set_value (s->eq_enable_controllable()->interface_to_internal (val), PBD::Controllable::NoGroup);
5236                         return 0;
5237                 }
5238         }
5239         return float_message(X_("/select/eq_enable"), 0, get_address (msg));
5240 }
5241
5242 int
5243 OSC::sel_eq_hpf_freq (float val, lo_message msg)
5244 {
5245         OSCSurface *sur = get_surface(get_address (msg));
5246         boost::shared_ptr<Stripable> s;
5247         if (sur->expand_enable) {
5248                 s = get_strip (sur->expand, get_address (msg));
5249         } else {
5250                 s = _select;
5251         }
5252         if (s) {
5253                 if (s->filter_freq_controllable(true)) {
5254                         s->filter_freq_controllable(true)->set_value (s->filter_freq_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
5255                         return 0;
5256                 }
5257         }
5258         return float_message(X_("/select/eq_hpf/freq"), 0, get_address (msg));
5259 }
5260
5261 int
5262 OSC::sel_eq_lpf_freq (float val, lo_message msg)
5263 {
5264         OSCSurface *sur = get_surface(get_address (msg));
5265         boost::shared_ptr<Stripable> s;
5266         if (sur->expand_enable) {
5267                 s = get_strip (sur->expand, get_address (msg));
5268         } else {
5269                 s = _select;
5270         }
5271         if (s) {
5272                 if (s->filter_freq_controllable(false)) {
5273                         s->filter_freq_controllable(false)->set_value (s->filter_freq_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
5274                         return 0;
5275                 }
5276         }
5277         return float_message(X_("/select/eq_lpf/freq"), 0, get_address (msg));
5278 }
5279
5280 int
5281 OSC::sel_eq_hpf_enable (float val, lo_message msg)
5282 {
5283         OSCSurface *sur = get_surface(get_address (msg));
5284         boost::shared_ptr<Stripable> s;
5285         if (sur->expand_enable) {
5286                 s = get_strip (sur->expand, get_address (msg));
5287         } else {
5288                 s = _select;
5289         }
5290         if (s) {
5291                 if (s->filter_enable_controllable(true)) {
5292                         s->filter_enable_controllable(true)->set_value (s->filter_enable_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
5293                         return 0;
5294                 }
5295         }
5296         return float_message(X_("/select/eq_hpf/enable"), 0, get_address (msg));
5297 }
5298
5299 int
5300 OSC::sel_eq_lpf_enable (float val, lo_message msg)
5301 {
5302         OSCSurface *sur = get_surface(get_address (msg));
5303         boost::shared_ptr<Stripable> s;
5304         if (sur->expand_enable) {
5305                 s = get_strip (sur->expand, get_address (msg));
5306         } else {
5307                 s = _select;
5308         }
5309         if (s) {
5310                 if (s->filter_enable_controllable(false)) {
5311                         s->filter_enable_controllable(false)->set_value (s->filter_enable_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
5312                         return 0;
5313                 }
5314         }
5315         return float_message(X_("/select/eq_lpf/enable"), 0, get_address (msg));
5316 }
5317
5318 int
5319 OSC::sel_eq_hpf_slope (float val, lo_message msg)
5320 {
5321         OSCSurface *sur = get_surface(get_address (msg));
5322         boost::shared_ptr<Stripable> s;
5323         if (sur->expand_enable) {
5324                 s = get_strip (sur->expand, get_address (msg));
5325         } else {
5326                 s = _select;
5327         }
5328         if (s) {
5329                 if (s->filter_slope_controllable(true)) {
5330                         s->filter_slope_controllable(true)->set_value (s->filter_slope_controllable(true)->interface_to_internal (val), PBD::Controllable::NoGroup);
5331                         return 0;
5332                 }
5333         }
5334         return float_message(X_("/select/eq_hpf/slope"), 0, get_address (msg));
5335 }
5336
5337 int
5338 OSC::sel_eq_lpf_slope (float val, lo_message msg)
5339 {
5340         OSCSurface *sur = get_surface(get_address (msg));
5341         boost::shared_ptr<Stripable> s;
5342         if (sur->expand_enable) {
5343                 s = get_strip (sur->expand, get_address (msg));
5344         } else {
5345                 s = _select;
5346         }
5347         if (s) {
5348                 if (s->filter_slope_controllable(false)) {
5349                         s->filter_slope_controllable(false)->set_value (s->filter_slope_controllable(false)->interface_to_internal (val), PBD::Controllable::NoGroup);
5350                         return 0;
5351                 }
5352         }
5353         return float_message(X_("/select/eq_lpf/slope"), 0, get_address (msg));
5354 }
5355
5356 int
5357 OSC::sel_eq_gain (int id, float val, lo_message msg)
5358 {
5359         OSCSurface *sur = get_surface(get_address (msg));
5360         boost::shared_ptr<Stripable> s;
5361         if (sur->expand_enable) {
5362                 s = get_strip (sur->expand, get_address (msg));
5363         } else {
5364                 s = _select;
5365         }
5366         if (s) {
5367                 if (id > 0) {
5368                         --id;
5369                 }
5370                 if (s->eq_gain_controllable (id)) {
5371                         s->eq_gain_controllable (id)->set_value (s->eq_gain_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
5372                         return 0;
5373                 }
5374         }
5375         return float_message_with_id (X_("/select/eq_gain"), id + 1, 0, sur->feedback[2], get_address (msg));
5376 }
5377
5378 int
5379 OSC::sel_eq_freq (int id, float val, lo_message msg)
5380 {
5381         OSCSurface *sur = get_surface(get_address (msg));
5382         boost::shared_ptr<Stripable> s;
5383         if (sur->expand_enable) {
5384                 s = get_strip (sur->expand, get_address (msg));
5385         } else {
5386                 s = _select;
5387         }
5388         if (s) {
5389                 if (id > 0) {
5390                         --id;
5391                 }
5392                 if (s->eq_freq_controllable (id)) {
5393                         s->eq_freq_controllable (id)->set_value (s->eq_freq_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
5394                         return 0;
5395                 }
5396         }
5397         return float_message_with_id (X_("/select/eq_freq"), id + 1, 0, sur->feedback[2], get_address (msg));
5398 }
5399
5400 int
5401 OSC::sel_eq_q (int id, float val, lo_message msg)
5402 {
5403         OSCSurface *sur = get_surface(get_address (msg));
5404         boost::shared_ptr<Stripable> s;
5405         if (sur->expand_enable) {
5406                 s = get_strip (sur->expand, get_address (msg));
5407         } else {
5408                 s = _select;
5409         }
5410         if (s) {
5411                 if (id > 0) {
5412                         --id;
5413                 }
5414                 if (s->eq_q_controllable (id)) {
5415                         s->eq_q_controllable (id)->set_value (s->eq_q_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
5416                         return 0;
5417                 }
5418         }
5419         return float_message_with_id (X_("/select/eq_q"), id + 1, 0, sur->feedback[2], get_address (msg));
5420 }
5421
5422 int
5423 OSC::sel_eq_shape (int id, float val, lo_message msg)
5424 {
5425         OSCSurface *sur = get_surface(get_address (msg));
5426         boost::shared_ptr<Stripable> s;
5427         if (sur->expand_enable) {
5428                 s = get_strip (sur->expand, get_address (msg));
5429         } else {
5430                 s = _select;
5431         }
5432         if (s) {
5433                 if (id > 0) {
5434                         --id;
5435                 }
5436                 if (s->eq_shape_controllable (id)) {
5437                         s->eq_shape_controllable (id)->set_value (s->eq_shape_controllable(id)->interface_to_internal (val), PBD::Controllable::NoGroup);
5438                         return 0;
5439                 }
5440         }
5441         return float_message_with_id (X_("/select/eq_shape"), id + 1, 0, sur->feedback[2], get_address (msg));
5442 }
5443
5444 // timer callbacks
5445 bool
5446 OSC::periodic (void)
5447 {
5448         if (observer_busy) {
5449                 return true;
5450         }
5451         if (!tick) {
5452                 Glib::usleep(100); // let flurry of signals subside
5453                 if (global_init) {
5454                         for (uint32_t it = 0; it < _surface.size(); it++) {
5455                                 OSCSurface* sur = &_surface[it];
5456                                 global_feedback (sur);
5457                         }
5458                         global_init = false;
5459                         tick = true;
5460                 }
5461                 if (bank_dirty) {
5462                         _recalcbanks ();
5463                         bank_dirty = false;
5464                         tick = true;
5465                 }
5466                 return true;
5467         }
5468
5469         if (scrub_speed != 0) {
5470                 // for those jog wheels that don't have 0 on release (touch), time out.
5471                 int64_t now = ARDOUR::get_microseconds ();
5472                 int64_t diff = now - scrub_time;
5473                 if (diff > 120000) {
5474                         scrub_speed = 0;
5475                         session->request_transport_speed (0);
5476                         // locate to the place PH was at last tick
5477                         session->request_locate (scrub_place, false);
5478                 }
5479         }
5480         for (uint32_t it = 0; it < _surface.size(); it++) {
5481                 OSCSurface* sur = &_surface[it];
5482                 OSCSelectObserver* so;
5483                 if ((so = dynamic_cast<OSCSelectObserver*>(sur->sel_obs)) != 0) {
5484                         so->tick ();
5485                 }
5486                 OSCCueObserver* co;
5487                 if ((co = dynamic_cast<OSCCueObserver*>(sur->cue_obs)) != 0) {
5488                         co->tick ();
5489                 }
5490                 OSCGlobalObserver* go;
5491                 if ((go = dynamic_cast<OSCGlobalObserver*>(sur->global_obs)) != 0) {
5492                         go->tick ();
5493                 }
5494                 for (uint32_t i = 0; i < sur->observers.size(); i++) {
5495                         OSCRouteObserver* ro;
5496                         if ((ro = dynamic_cast<OSCRouteObserver*>(sur->observers[i])) != 0) {
5497                                 ro->tick ();
5498                         }
5499                 }
5500
5501         }
5502         for (FakeTouchMap::iterator x = _touch_timeout.begin(); x != _touch_timeout.end();) {
5503                 _touch_timeout[(*x).first] = (*x).second - 1;
5504                 if (!(*x).second) {
5505                         boost::shared_ptr<ARDOUR::AutomationControl> ctrl = (*x).first;
5506                         // turn touch off
5507                         ctrl->stop_touch (ctrl->session().transport_sample());
5508                         _touch_timeout.erase (x++);
5509                 } else {
5510                         x++;
5511                 }
5512         }
5513         return true;
5514 }
5515
5516 XMLNode&
5517 OSC::get_state ()
5518 {
5519         XMLNode& node (ControlProtocol::get_state());
5520         node.set_property (X_("debugmode"), (int32_t) _debugmode); // TODO: enum2str
5521         node.set_property (X_("address-only"), address_only);
5522         node.set_property (X_("remote-port"), remote_port);
5523         node.set_property (X_("banksize"), default_banksize);
5524         node.set_property (X_("striptypes"), default_strip);
5525         node.set_property (X_("feedback"), default_feedback);
5526         node.set_property (X_("gainmode"), default_gainmode);
5527         node.set_property (X_("send-page-size"), default_send_size);
5528         node.set_property (X_("plug-page-size"), default_plugin_size);
5529         return node;
5530 }
5531
5532 int
5533 OSC::set_state (const XMLNode& node, int version)
5534 {
5535         if (ControlProtocol::set_state (node, version)) {
5536                 return -1;
5537         }
5538         int32_t debugmode;
5539         if (node.get_property (X_("debugmode"), debugmode)) {
5540                 _debugmode = OSCDebugMode (debugmode);
5541         }
5542
5543         node.get_property (X_("address-only"), address_only);
5544         node.get_property (X_("remote-port"), remote_port);
5545         node.get_property (X_("banksize"), default_banksize);
5546         node.get_property (X_("striptypes"), default_strip);
5547         node.get_property (X_("feedback"), default_feedback);
5548         node.get_property (X_("gainmode"), default_gainmode);
5549         node.get_property (X_("send-page-size"), default_send_size);
5550         node.get_property (X_("plugin-page-size"), default_plugin_size);
5551
5552         global_init = true;
5553         tick = false;
5554
5555         return 0;
5556 }
5557
5558 // predicate for sort call in get_sorted_stripables
5559 struct StripableByPresentationOrder
5560 {
5561         bool operator () (const boost::shared_ptr<Stripable> & a, const boost::shared_ptr<Stripable> & b) const
5562         {
5563                 return a->presentation_info().order() < b->presentation_info().order();
5564         }
5565
5566         bool operator () (const Stripable & a, const Stripable & b) const
5567         {
5568                 return a.presentation_info().order() < b.presentation_info().order();
5569         }
5570
5571         bool operator () (const Stripable * a, const Stripable * b) const
5572         {
5573                 return a->presentation_info().order() < b->presentation_info().order();
5574         }
5575 };
5576
5577 OSC::Sorted
5578 OSC::get_sorted_stripables(std::bitset<32> types, bool cue, uint32_t custom, Sorted my_list)
5579 {
5580         Sorted sorted;
5581         StripableList stripables;
5582         StripableList custom_list;
5583
5584         // fetch all stripables
5585         session->get_stripables (stripables);
5586         if (custom) {
5587                 uint32_t nstps = my_list.size ();
5588                 // check each custom strip to see if it still exists
5589                 boost::shared_ptr<Stripable> s;
5590                 for (uint32_t i = 0; i < nstps; i++) {
5591                         bool exists = false;
5592                         s = my_list[i];
5593                         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5594                                 boost::shared_ptr<Stripable> sl = *it;
5595                                 if (s == sl) {
5596                                         exists = true;
5597                                         break;
5598                                 }
5599                         }
5600                         if(!exists) {
5601                                 my_list[i] = boost::shared_ptr<Stripable>();
5602                         } else {
5603                                 custom_list.push_back (s);
5604                         }
5605                 }
5606                 if (custom == 1) {
5607                         return my_list;
5608                 } else {
5609                         stripables = custom_list;
5610                 }
5611         }
5612         // Look for stripables that match bit in sur->strip_types
5613         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
5614
5615                 boost::shared_ptr<Stripable> s = *it;
5616                 if (!s) {
5617                         break;
5618                 }
5619                 if (custom == 2) {
5620                         // banking off use all valid custom strips
5621                         sorted.push_back (s);
5622                 } else
5623                 if ((!cue) && (!types[9]) && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5624                         // do nothing... skip it
5625                 } else if (types[8] && (s->is_selected())) {
5626                         sorted.push_back (s);
5627                 } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
5628                         sorted.push_back (s);
5629                 } else if (s->is_master() || s->is_monitor() || s->is_auditioner()) {
5630                         // do nothing for these either (we add them later)
5631                 } else {
5632                         if (types[0] && boost::dynamic_pointer_cast<AudioTrack>(s)) {
5633                                 sorted.push_back (s);
5634                         } else if (types[1] && boost::dynamic_pointer_cast<MidiTrack>(s)) {
5635                                 sorted.push_back (s);
5636                         } else if (types[4] && boost::dynamic_pointer_cast<VCA>(s)) {
5637                                 sorted.push_back (s);
5638                         } else
5639 #ifdef MIXBUS
5640                         if (types[2] && Profile->get_mixbus() && s->mixbus()) {
5641                                 sorted.push_back (s);
5642                         } else
5643                         if (types[7] && boost::dynamic_pointer_cast<Route>(s) && !boost::dynamic_pointer_cast<Track>(s)) {
5644                                 if (Profile->get_mixbus() && !s->mixbus()) {
5645                                         sorted.push_back (s);
5646                                 }
5647                         } else
5648 #endif
5649                         if ((types[2] || types[3] || types[7]) && boost::dynamic_pointer_cast<Route>(s) && !boost::dynamic_pointer_cast<Track>(s)) {
5650                                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route>(s);
5651                                 if (!(s->presentation_info().flags() & PresentationInfo::MidiBus)) {
5652                                         // note some older sessions will show midibuses as busses
5653                                         if (r->direct_feeds_according_to_reality (session->master_out())) {
5654                                                 // this is a bus
5655                                                 if (types[2]) {
5656                                                         sorted.push_back (s);
5657                                                 }
5658                                         } else {
5659                                                 // this is an Aux out
5660                                                 if (types[7]) {
5661                                                         sorted.push_back (s);
5662                                                 }
5663                                         }
5664                                 } else if (types[3]) {
5665                                                 sorted.push_back (s);
5666                                 }
5667                         }
5668                 }
5669         }
5670         if (!custom || (custom & 0x2)) {
5671                 sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
5672         }
5673         if (!custom) {
5674                 // Master/Monitor might be anywhere... we put them at the end - Sorry ;)
5675                 if (types[5]) {
5676                         sorted.push_back (session->master_out());
5677                 }
5678                 if (types[6]) {
5679                         if (session->monitor_out()) {
5680                                 sorted.push_back (session->monitor_out());
5681                         }
5682                 }
5683         }
5684         return sorted;
5685 }
5686
5687 int
5688 OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo_message msg)
5689 {
5690         OSCSurface *s = get_surface(get_address (msg), true);
5691         s->bank_size = 0;
5692         float value = 0;
5693         if (argc == 1) {
5694                 if (types[0] == 'f') {
5695                         value = argv[0]->f;
5696                 } else if (types[0] == 'i') {
5697                         value = (float) argv[0]->i;
5698                 }
5699         }
5700         int ret = 1; /* unhandled */
5701         if (!strncmp (path, X_("/cue/aux"), 8)) {
5702                 // set our Aux bus
5703                 if (argc) {
5704                         if (value) {
5705                                 ret = cue_set ((uint32_t) value, msg);
5706                         } else {
5707                                 ret = 0;
5708                         }
5709                 }
5710         }
5711         else if (!strncmp (path, X_("/cue/connect"), 12)) {
5712                 // Connect to default Aux bus
5713                 if ((!argc) || argv[0]->f || argv[0]->i) {
5714                         ret = cue_set (1, msg);
5715                 } else {
5716                         ret = 0;
5717                 }
5718         }
5719         else if (!strncmp (path, X_("/cue/next_aux"), 13)) {
5720                 // switch to next Aux bus
5721                 if ((!argc) || argv[0]->f || argv[0]->i) {
5722                         ret = cue_next (msg);
5723                 } else {
5724                         ret = 0;
5725                 }
5726         }
5727         else if (!strncmp (path, X_("/cue/previous_aux"), 17)) {
5728                 // switch to previous Aux bus
5729                 if ((!argc) || argv[0]->f || argv[0]->i) {
5730                         ret = cue_previous (msg);
5731                 } else {
5732                         ret = 0;
5733                 }
5734         }
5735         else if (!strncmp (path, X_("/cue/send/fader/"), 16) && strlen (path) > 16) {
5736                 if (argc == 1) {
5737                         int id = atoi (&path[16]);
5738                         ret = cue_send_fader (id, value, msg);
5739                 }
5740         }
5741         else if (!strncmp (path, X_("/cue/send/enable/"), 17) && strlen (path) > 17) {
5742                 if (argc == 1) {
5743                         int id = atoi (&path[17]);
5744                         ret = cue_send_enable (id, value, msg);
5745                 }
5746         }
5747         else if (!strncmp (path, X_("/cue/fader"), 10)) {
5748                 if (argc == 1) {
5749                         ret = cue_aux_fader (value, msg);
5750                 }
5751         }
5752         else if (!strncmp (path, X_("/cue/mute"), 9)) {
5753                 if (argc == 1) {
5754                         ret = cue_aux_mute (value, msg);
5755                 }
5756         }
5757
5758         return ret;
5759 }
5760
5761 int
5762 OSC::cue_set (uint32_t aux, lo_message msg)
5763 {
5764
5765         return _cue_set (aux, get_address (msg));
5766 }
5767
5768 int
5769 OSC::_cue_set (uint32_t aux, lo_address addr)
5770 {
5771         int ret = 1;
5772         OSCSurface *s = get_surface(addr, true);
5773         s->bank_size = 0;
5774         s->strip_types = 128;
5775         s->feedback = 0;
5776         s->gainmode = 1;
5777         s->cue = true;
5778         s->strips = get_sorted_stripables(s->strip_types, s->cue, false, s->custom_strips);
5779
5780         s->nstrips = s->strips.size();
5781
5782         if (aux < 1) {
5783                 aux = 1;
5784         } else if (aux > s->nstrips) {
5785                 aux = s->nstrips;
5786         }
5787         s->aux = aux;
5788         // get a list of Auxes
5789         for (uint32_t n = 0; n < s->nstrips; ++n) {
5790                 boost::shared_ptr<Stripable> stp = s->strips[n];
5791                 if (stp) {
5792                         text_message (string_compose (X_("/cue/name/%1"), n+1), stp->name(), addr);
5793                         if (aux == n+1) {
5794                                 // aux must be at least one
5795
5796                                 stp->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::_cue_set, this, aux, addr), this);
5797                                 // make a list of stripables with sends that go to this bus
5798                                 s->sends = cue_get_sorted_stripables(stp, aux, addr);
5799                                 if (s->cue_obs) {
5800                                         s->cue_obs->refresh_strip (stp, s->sends, true);
5801                                 } else {
5802                                         // start cue observer
5803                                         OSCCueObserver* co = new OSCCueObserver (*this, s);
5804                                         s->cue_obs = co;
5805                                 }
5806                                 ret = 0;
5807                         }
5808
5809                 }
5810         }
5811
5812         return ret;
5813 }
5814
5815 int
5816 OSC::cue_next (lo_message msg)
5817 {
5818         OSCSurface *s = get_surface(get_address (msg), true);
5819         int ret = 1;
5820
5821         if (!s->cue) {
5822                 ret = cue_set (1, msg);
5823         }
5824         if (s->aux < s->nstrips) {
5825                 ret = cue_set (s->aux + 1, msg);
5826         } else {
5827                 ret = cue_set (s->nstrips, msg);
5828         }
5829         return ret;
5830 }
5831
5832 int
5833 OSC::cue_previous (lo_message msg)
5834 {
5835         OSCSurface *s = get_surface(get_address (msg), true);
5836         int ret = 1;
5837         if (!s->cue) {
5838                 ret = cue_set (1, msg);
5839         }
5840         if (s->aux > 1) {
5841                 ret = cue_set (s->aux - 1, msg);
5842         } else {
5843                 ret = cue_set (1, msg);
5844         }
5845         return ret;
5846 }
5847
5848 boost::shared_ptr<Send>
5849 OSC::cue_get_send (uint32_t id, lo_address addr)
5850 {
5851         OSCSurface *s = get_surface(addr, true);
5852         if (id && s->aux > 0 && id <= s->sends.size()) {
5853                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s->sends[id - 1]);
5854                 boost::shared_ptr<Stripable> aux = get_strip (s->aux, addr);
5855                 if (r && aux) {
5856                         return r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
5857                 }
5858         }
5859         return boost::shared_ptr<Send>();
5860
5861 }
5862
5863 int
5864 OSC::cue_aux_fader (float position, lo_message msg)
5865 {
5866         if (!session) return -1;
5867
5868         OSCSurface *sur = get_surface(get_address (msg), true);
5869         if (sur->cue) {
5870                 if (sur->aux) {
5871                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5872
5873                         if (s) {
5874                                 if (s->gain_control()) {
5875                                         s->gain_control()->set_value (s->gain_control()->interface_to_internal (position), PBD::Controllable::NoGroup);
5876                                         return 0;
5877                                 }
5878                         }
5879                 }
5880         }
5881         float_message (X_("/cue/fader"), 0, get_address (msg));
5882         return -1;
5883 }
5884
5885 int
5886 OSC::cue_aux_mute (float state, lo_message msg)
5887 {
5888         if (!session) return -1;
5889
5890         OSCSurface *sur = get_surface(get_address (msg), true);
5891         if (sur->cue) {
5892                 if (sur->aux) {
5893                         boost::shared_ptr<Stripable> s = get_strip (sur->aux, get_address (msg));
5894                         if (s) {
5895                                 if (s->mute_control()) {
5896                                         s->mute_control()->set_value (state ? 1.0 : 0.0, PBD::Controllable::NoGroup);
5897                                         return 0;
5898                                 }
5899                         }
5900                 }
5901         }
5902         float_message (X_("/cue/mute"), 0, get_address (msg));
5903         return -1;
5904 }
5905
5906 int
5907 OSC::cue_send_fader (uint32_t id, float val, lo_message msg)
5908 {
5909         if (!session) {
5910                 return -1;
5911         }
5912         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5913         if (s) {
5914                 if (s->gain_control()) {
5915                         s->gain_control()->set_value (s->gain_control()->interface_to_internal(val), PBD::Controllable::NoGroup);
5916                         return 0;
5917                 }
5918         }
5919         float_message (string_compose (X_("/cue/send/fader/%1"), id), 0, get_address (msg));
5920         return -1;
5921 }
5922
5923 int
5924 OSC::cue_send_enable (uint32_t id, float state, lo_message msg)
5925 {
5926         if (!session)
5927                 return -1;
5928         boost::shared_ptr<Send> s = cue_get_send (id, get_address (msg));
5929         if (s) {
5930                 if (state) {
5931                         s->activate ();
5932                 } else {
5933                         s->deactivate ();
5934                 }
5935                 return 0;
5936         }
5937         float_message (string_compose (X_("/cue/send/enable/%1"), id), 0, get_address (msg));
5938         return -1;
5939 }
5940
5941 // generic send message
5942 int
5943 OSC::float_message (string path, float val, lo_address addr)
5944 {
5945
5946         lo_message reply;
5947         reply = lo_message_new ();
5948         lo_message_add_float (reply, (float) val);
5949
5950         lo_send_message (addr, path.c_str(), reply);
5951         lo_message_free (reply);
5952
5953         return 0;
5954 }
5955
5956 int
5957 OSC::float_message_with_id (std::string path, uint32_t ssid, float value, bool in_line, lo_address addr)
5958 {
5959         lo_message msg = lo_message_new ();
5960         if (in_line) {
5961                 path = string_compose ("%1/%2", path, ssid);
5962         } else {
5963                 lo_message_add_int32 (msg, ssid);
5964         }
5965         lo_message_add_float (msg, value);
5966
5967         lo_send_message (addr, path.c_str(), msg);
5968         lo_message_free (msg);
5969         return 0;
5970 }
5971
5972 int
5973 OSC::int_message (string path, int val, lo_address addr)
5974 {
5975
5976         lo_message reply;
5977         reply = lo_message_new ();
5978         lo_message_add_int32 (reply, (float) val);
5979
5980         lo_send_message (addr, path.c_str(), reply);
5981         lo_message_free (reply);
5982
5983         return 0;
5984 }
5985
5986 int
5987 OSC::int_message_with_id (std::string path, uint32_t ssid, int value, bool in_line, lo_address addr)
5988 {
5989         lo_message msg = lo_message_new ();
5990         if (in_line) {
5991                 path = string_compose ("%1/%2", path, ssid);
5992         } else {
5993                 lo_message_add_int32 (msg, ssid);
5994         }
5995         lo_message_add_int32 (msg, value);
5996
5997         lo_send_message (addr, path.c_str(), msg);
5998         lo_message_free (msg);
5999         return 0;
6000 }
6001
6002 int
6003 OSC::text_message (string path, string val, lo_address addr)
6004 {
6005
6006         lo_message reply;
6007         reply = lo_message_new ();
6008         lo_message_add_string (reply, val.c_str());
6009
6010         lo_send_message (addr, path.c_str(), reply);
6011         lo_message_free (reply);
6012
6013         return 0;
6014 }
6015
6016 int
6017 OSC::text_message_with_id (std::string path, uint32_t ssid, std::string val, bool in_line, lo_address addr)
6018 {
6019         lo_message msg = lo_message_new ();
6020         if (in_line) {
6021                 path = string_compose ("%1/%2", path, ssid);
6022         } else {
6023                 lo_message_add_int32 (msg, ssid);
6024         }
6025
6026         lo_message_add_string (msg, val.c_str());
6027
6028         lo_send_message (addr, path.c_str(), msg);
6029         lo_message_free (msg);
6030         return 0;
6031 }
6032
6033 // we have to have a sorted list of stripables that have sends pointed at our aux
6034 // we can use the one in osc.cc to get an aux list
6035 OSC::Sorted
6036 OSC::cue_get_sorted_stripables(boost::shared_ptr<Stripable> aux, uint32_t id, lo_message msg)
6037 {
6038         Sorted sorted;
6039         // fetch all stripables
6040         StripableList stripables;
6041
6042         session->get_stripables (stripables);
6043
6044         // Look for stripables that have a send to aux
6045         for (StripableList::iterator it = stripables.begin(); it != stripables.end(); ++it) {
6046
6047                 boost::shared_ptr<Stripable> s = *it;
6048                 // we only want routes
6049                 boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (s);
6050                 if (r) {
6051                         r->processors_changed.connect  (*this, MISSING_INVALIDATOR, boost::bind (&OSC::recalcbanks, this), this);
6052                         boost::shared_ptr<Send> snd = r->internal_send_for (boost::dynamic_pointer_cast<Route> (aux));
6053                         if (snd) { // test for send to aux
6054                                 sorted.push_back (s);
6055                                 s->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::cue_set, this, id, msg), this);
6056                         }
6057                 }
6058
6059
6060         }
6061         sort (sorted.begin(), sorted.end(), StripableByPresentationOrder());
6062
6063         return sorted;
6064 }
6065