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