OSC: take care of track order changes too. Deal with mixer topology changes one time.
[ardour.git] / libs / surfaces / osc / osc.h
1 /*
2  * Copyright (C) 2006-2009 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 #ifndef ardour_osc_h
21 #define ardour_osc_h
22
23 #include <string>
24 #include <vector>
25 #include <bitset>
26
27 #include <sys/time.h>
28 #include <pthread.h>
29
30 #include <boost/shared_ptr.hpp>
31
32 #include <lo/lo.h>
33
34 #include <glibmm/main.h>
35
36 #define ABSTRACT_UI_EXPORTS
37 #include "pbd/abstract_ui.h"
38
39 #include "ardour/types.h"
40 #include "control_protocol/control_protocol.h"
41
42 #include "i18n.h"
43
44 class OSCControllable;
45 class OSCRouteObserver;
46 class OSCGlobalObserver;
47 class OSCSelectObserver;
48
49 namespace ARDOUR {
50 class Session;
51 class Route;
52 }
53
54 /* this is mostly a placeholder because I suspect that at some
55    point we will want to add more members to accomodate
56    certain types of requests to the OSC UI
57 */
58
59 namespace ArdourSurface {
60
61 struct OSCUIRequest : public BaseUI::BaseRequestObject {
62   public:
63         OSCUIRequest () {}
64         ~OSCUIRequest() {}
65 };
66
67 class OSC : public ARDOUR::ControlProtocol, public AbstractUI<OSCUIRequest>
68 {
69   public:
70         OSC (ARDOUR::Session&, uint32_t port);
71         virtual ~OSC();
72
73         static OSC* instance() { return _instance; }
74
75         XMLNode& get_state ();
76         int set_state (const XMLNode&, int version);
77
78         bool has_editor () const { return true; }
79         void* get_gui () const;
80         void  tear_down_gui ();
81
82         int set_active (bool yn);
83         bool get_active () const;
84         int set_feedback (bool yn);
85         bool get_feedback () const;
86
87
88         int start ();
89         int stop ();
90
91         static void* request_factory (uint32_t);
92
93         enum OSCDebugMode {
94                 Off,
95                 Unhandled,
96                 All
97         };
98
99 // keep a surface's global setup by remote server url
100         struct OSCSurface {
101         public:
102                 std::string remote_url;         // the url these setting belong to
103                 uint32_t bank;                          // current bank
104                 uint32_t bank_size;                     // size of banks for this surface
105                 std::bitset<32> strip_types;// what strip types are a part of this bank
106                 uint32_t nstrips;                       // how many strips are there for strip_types
107                 std::bitset<32> feedback;       // What is fed back? strips/meters/timecode/bar_beat/global
108                 int gainmode;                           // what kind of faders do we have Gain db or position 0 to 1023?
109                 uint32_t surface_sel;           // which strip within the bank is locally selected
110                 OSCSelectObserver* sel_obs;     // So we can sync select feedback with selected channel
111                 //StripableList strips; //list of stripables for the current bank
112         };
113                 /*
114                  * Reminder of what strip_types there are
115                  * XXX these have changed!!!
116                 AudioTrack = 0x1,
117                 MidiTrack = 0x2,
118                 AudioBus = 0x4,
119                 MidiBus = 0x8,
120                 VCA = 0x10,
121                 MasterOut = 0x800,
122                 MonitorOut = 0x1000,
123                 Auditioner = 0x2000,
124                 Selected = 0x4000,
125                 Hidden = 0x8000,
126                 */
127                 /*
128                  * feedback bits:
129                  * [0] - Strips - buttons
130                  * [1] - Strips - variables (pots/faders)
131                  * [2] - Strips - Send SSID as path extension
132                  * [3] - Send heart beat to surface
133                  * [4] - Send feedback for master/global buttons/variables
134                  * [5] - Send Bar and Beat
135                  * [6] - Send Time code
136                  * [7] - Send metering as dB or positional depending on gainmode
137                  * [8] - Send metering as 16 bits (led strip)
138                  * [9] - Send signal present (signal greater than -20dB)
139                  * [10] - Selection is local
140                  */
141
142
143 // storage for  each surface's settings
144         typedef std::vector<OSCSurface> Surface;
145         Surface _surface;
146
147         std::string get_server_url ();
148         void set_debug_mode (OSCDebugMode m) { _debugmode = m; }
149         OSCDebugMode get_debug_mode () { return _debugmode; }
150
151   protected:
152         void thread_init ();
153         void do_request (OSCUIRequest*);
154
155         GSource* local_server;
156         GSource* remote_server;
157
158         bool osc_input_handler (Glib::IOCondition, lo_server);
159
160   private:
161         uint32_t _port;
162         volatile bool _ok;
163         volatile bool _shutdown;
164         lo_server _osc_server;
165         lo_server _osc_unix_server;
166         std::string _osc_unix_socket_path;
167         std::string _osc_url_file;
168         bool _send_route_changes;
169         OSCDebugMode _debugmode;
170         bool tick;
171         bool bank_dirty;
172
173         void register_callbacks ();
174
175         void route_added (ARDOUR::RouteList&);
176
177         // Handlers for "Application Hook" signals
178         void session_loaded (ARDOUR::Session&);
179         void session_exported (std::string, std::string);
180
181         // end "Application Hook" handles
182
183         std::string get_unix_server_url ();
184         OSCSurface * get_surface (lo_address addr);
185         uint32_t get_sid (uint32_t rid, lo_address addr);
186         uint32_t get_rid (uint32_t sid, lo_address addr);
187         void global_feedback (std::bitset<32> feedback, lo_address msg, uint32_t gainmode);
188
189         void send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg);
190         void current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg);
191
192         int current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
193
194         int catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data);
195         static int _catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
196
197         void routes_list (lo_message msg);
198         void transport_frame (lo_message msg);
199         void transport_speed (lo_message msg);
200         void record_enabled (lo_message msg);
201
202 #define OSC_DEBUG \
203         if (_debugmode == All) { \
204                 debugmsg (dgettext(PACKAGE, "OSC"), path, types, argv, argc); \
205         }
206
207 #define PATH_CALLBACK_MSG(name)                                 \
208         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
209                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
210         } \
211         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
212                 OSC_DEBUG;              \
213                 if (argc > 0 && !strcmp (types, "f") && argv[0]->f != 1.0) { return 0; } \
214                 name (data);            \
215                 return 0;               \
216         }
217
218         PATH_CALLBACK_MSG(routes_list);
219         PATH_CALLBACK_MSG(transport_frame);
220         PATH_CALLBACK_MSG(transport_speed);
221         PATH_CALLBACK_MSG(record_enabled);
222         PATH_CALLBACK_MSG(bank_up);
223         PATH_CALLBACK_MSG(bank_down);
224
225 #define PATH_CALLBACK(name) \
226         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
227                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
228         } \
229         int cb_ ## name (const char *path, const char *types, lo_arg ** argv, int argc, void *) { \
230                 OSC_DEBUG;              \
231                 if (argc > 0 && !strcmp (types, "f") && argv[0]->f != 1.0) { return 0; } \
232                 name (); \
233                 return 0; \
234         }
235
236         PATH_CALLBACK(add_marker);
237         PATH_CALLBACK(loop_toggle);
238         PATH_CALLBACK(goto_start);
239         PATH_CALLBACK(goto_end);
240         PATH_CALLBACK(rewind);
241         PATH_CALLBACK(ffwd);
242         PATH_CALLBACK(transport_stop);
243         PATH_CALLBACK(transport_play);
244         PATH_CALLBACK(save_state);
245         PATH_CALLBACK(prev_marker);
246         PATH_CALLBACK(next_marker);
247         PATH_CALLBACK(undo);
248         PATH_CALLBACK(redo);
249         PATH_CALLBACK(toggle_punch_in);
250         PATH_CALLBACK(toggle_punch_out);
251         PATH_CALLBACK(rec_enable_toggle);
252         PATH_CALLBACK(toggle_all_rec_enables);
253         PATH_CALLBACK(all_tracks_rec_in);
254         PATH_CALLBACK(all_tracks_rec_out);
255         PATH_CALLBACK(remove_marker_at_playhead);
256         PATH_CALLBACK(mark_in);
257         PATH_CALLBACK(mark_out);
258         PATH_CALLBACK(toggle_click);
259         PATH_CALLBACK(midi_panic);
260         PATH_CALLBACK(toggle_roll);
261         PATH_CALLBACK(stop_forget);
262         PATH_CALLBACK(set_punch_range);
263         PATH_CALLBACK(set_loop_range);
264         PATH_CALLBACK(set_session_range);
265         PATH_CALLBACK(toggle_monitor_mute);
266         PATH_CALLBACK(toggle_monitor_dim);
267         PATH_CALLBACK(toggle_monitor_mono);
268         PATH_CALLBACK(quick_snapshot_stay);
269         PATH_CALLBACK(quick_snapshot_switch);
270         PATH_CALLBACK(fit_1_track);
271         PATH_CALLBACK(fit_2_tracks);
272         PATH_CALLBACK(fit_4_tracks);
273         PATH_CALLBACK(fit_8_tracks);
274         PATH_CALLBACK(fit_16_tracks);
275         PATH_CALLBACK(fit_32_tracks);
276         PATH_CALLBACK(fit_all_tracks);
277         PATH_CALLBACK(zoom_100_ms);
278         PATH_CALLBACK(zoom_1_sec);
279         PATH_CALLBACK(zoom_10_sec);
280         PATH_CALLBACK(zoom_1_min);
281         PATH_CALLBACK(zoom_5_min);
282         PATH_CALLBACK(zoom_10_min);
283         PATH_CALLBACK(zoom_to_session);
284         PATH_CALLBACK(temporal_zoom_in);
285         PATH_CALLBACK(temporal_zoom_out);
286         PATH_CALLBACK(scroll_up_1_track);
287         PATH_CALLBACK(scroll_dn_1_track);
288         PATH_CALLBACK(scroll_up_1_page);
289         PATH_CALLBACK(scroll_dn_1_page);
290
291 #define PATH_CALLBACK1(name,type,optional)                                      \
292         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
293                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
294         } \
295         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *) { \
296                 OSC_DEBUG;              \
297                 if (argc > 0) {                                         \
298                         name (optional argv[0]->type);          \
299                 }                                                       \
300                 return 0;                                               \
301         }
302
303         PATH_CALLBACK1(set_transport_speed,f,);
304         PATH_CALLBACK1(access_action,s,&);
305
306         PATH_CALLBACK1(jump_by_bars,f,);
307         PATH_CALLBACK1(jump_by_seconds,f,);
308         PATH_CALLBACK1(master_set_gain,f,);
309         PATH_CALLBACK1(master_set_fader,i,);
310         PATH_CALLBACK1(master_set_trim,f,);
311         PATH_CALLBACK1(master_set_mute,i,);
312         PATH_CALLBACK1(monitor_set_gain,f,);
313         PATH_CALLBACK1(monitor_set_fader,i,);
314
315 #define PATH_CALLBACK1_MSG(name,arg1type)                       \
316         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
317                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
318         } \
319         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
320                 OSC_DEBUG;              \
321                 if (argc > 0) {                                         \
322                         name (argv[0]->arg1type, data); \
323                 }                                                       \
324                 return 0;                                               \
325         }
326
327         // pan position needs message info to send feedback
328         PATH_CALLBACK1_MSG(master_set_pan_stereo_position,f);
329
330         PATH_CALLBACK1_MSG(set_surface_bank_size,i);
331         PATH_CALLBACK1_MSG(set_surface_strip_types,i);
332         PATH_CALLBACK1_MSG(set_surface_feedback,i);
333         PATH_CALLBACK1_MSG(set_surface_gainmode,i);
334         PATH_CALLBACK1_MSG(sel_recenable,i);
335         PATH_CALLBACK1_MSG(sel_recsafe,i);
336         PATH_CALLBACK1_MSG(sel_mute,i);
337         PATH_CALLBACK1_MSG(sel_solo,i);
338         PATH_CALLBACK1_MSG(sel_solo_iso,i);
339         PATH_CALLBACK1_MSG(sel_solo_safe,i);
340         PATH_CALLBACK1_MSG(sel_monitor_input,i);
341         PATH_CALLBACK1_MSG(sel_monitor_disk,i);
342         PATH_CALLBACK1_MSG(sel_phase,i);
343         PATH_CALLBACK1_MSG(sel_gain,f);
344         PATH_CALLBACK1_MSG(sel_fader,f);
345         PATH_CALLBACK1_MSG(sel_trim,f);
346         PATH_CALLBACK1_MSG(sel_pan_position,f);
347         PATH_CALLBACK1_MSG(sel_pan_width,f);
348
349 #define PATH_CALLBACK2(name,arg1type,arg2type)                  \
350         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
351                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
352         } \
353         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *) { \
354                 OSC_DEBUG;              \
355                 if (argc > 1) {                                         \
356                         name (argv[0]->arg1type, argv[1]->arg2type); \
357                 }                                                       \
358                 return 0;                                               \
359         }
360
361 #define PATH_CALLBACK2_MSG(name,arg1type,arg2type)                      \
362         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
363                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
364         } \
365         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
366                 OSC_DEBUG;              \
367                 if (argc > 1) {                                         \
368                         name (argv[0]->arg1type, argv[1]->arg2type, data); \
369                 }                                                       \
370                 return 0;                                               \
371         }
372
373 #define PATH_CALLBACK3(name,arg1type,arg2type,arg3type)                \
374         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
375                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
376         } \
377         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
378                 OSC_DEBUG;              \
379                 if (argc > 1) {                                                \
380                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type, data); \
381                 }                                                      \
382                return 0;                                               \
383        }
384
385 #define PATH_CALLBACK4(name,arg1type,arg2type,arg3type,arg4type)               \
386         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
387                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
388         } \
389         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
390                 OSC_DEBUG;              \
391                 if (argc > 1) {                                                \
392                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type,argv[3]->arg4type, data); \
393                 }                                                      \
394                return 0;                                               \
395        }
396
397         PATH_CALLBACK2_MSG(sel_sendgain,i,f);
398         PATH_CALLBACK2_MSG(sel_sendfader,i,f);
399         PATH_CALLBACK2_MSG(sel_sendenable,i,f);
400
401         PATH_CALLBACK4(set_surface,i,i,i,i);
402         PATH_CALLBACK2(locate,i,i);
403         PATH_CALLBACK2(loop_location,i,i);
404         PATH_CALLBACK2_MSG(route_mute,i,i);
405         PATH_CALLBACK2_MSG(route_solo,i,i);
406         PATH_CALLBACK2_MSG(route_solo_iso,i,i);
407         PATH_CALLBACK2_MSG(route_solo_safe,i,i);
408         PATH_CALLBACK2_MSG(route_recenable,i,i);
409         PATH_CALLBACK2_MSG(route_recsafe,i,i);
410         PATH_CALLBACK2_MSG(route_monitor_input,i,i);
411         PATH_CALLBACK2_MSG(route_monitor_disk,i,i);
412         PATH_CALLBACK2_MSG(strip_phase,i,i);
413         PATH_CALLBACK2_MSG(strip_select,i,i);
414         PATH_CALLBACK2_MSG(strip_gui_select,i,i);
415         PATH_CALLBACK2_MSG(route_set_gain_abs,i,f);
416         PATH_CALLBACK2_MSG(route_set_gain_dB,i,f);
417         PATH_CALLBACK2_MSG(route_set_gain_fader,i,f);
418         PATH_CALLBACK2_MSG(route_set_trim_dB,i,f);
419         PATH_CALLBACK2_MSG(route_set_pan_stereo_position,i,f);
420         PATH_CALLBACK2_MSG(route_set_pan_stereo_width,i,f);
421         PATH_CALLBACK3(route_set_send_gain_abs,i,i,f);
422         PATH_CALLBACK3(route_set_send_gain_dB,i,i,f);
423         PATH_CALLBACK3(route_set_send_fader,i,i,f);
424         PATH_CALLBACK3(route_set_send_enable,i,i,f);
425         PATH_CALLBACK4(route_plugin_parameter,i,i,i,f);
426         PATH_CALLBACK3(route_plugin_parameter_print,i,i,i);
427
428         int route_mute (int rid, int yn, lo_message msg);
429         int route_solo (int rid, int yn, lo_message msg);
430         int route_solo_iso (int rid, int yn, lo_message msg);
431         int route_solo_safe (int rid, int yn, lo_message msg);
432         int route_recenable (int rid, int yn, lo_message msg);
433         int route_recsafe (int ssid, int yn, lo_message msg);
434         int route_monitor_input (int rid, int yn, lo_message msg);
435         int route_monitor_disk (int rid, int yn, lo_message msg);
436         int strip_phase (int rid, int yn, lo_message msg);
437         int strip_select (int rid, int yn, lo_message msg);
438         int _strip_select (int rid, lo_address addr);
439         int strip_gui_select (int rid, int yn, lo_message msg);
440         int route_set_gain_abs (int rid, float level, lo_message msg);
441         int route_set_gain_dB (int rid, float dB, lo_message msg);
442         int route_set_gain_fader (int rid, float pos, lo_message msg);
443         int route_set_trim_abs (int rid, float level, lo_message msg);
444         int route_set_trim_dB (int rid, float dB, lo_message msg);
445         int route_set_pan_stereo_position (int rid, float left_right_fraction, lo_message msg);
446         int route_set_pan_stereo_width (int rid, float percent, lo_message msg);
447         int route_set_send_gain_abs (int rid, int sid, float val, lo_message msg);
448         int route_set_send_gain_dB (int rid, int sid, float val, lo_message msg);
449         int route_set_send_fader (int rid, int sid, float val, lo_message msg);
450         int route_set_send_enable (int rid, int sid, float val, lo_message msg);
451         int route_plugin_parameter (int rid, int piid,int par, float val, lo_message msg);
452         int route_plugin_parameter_print (int rid, int piid,int par, lo_message msg);
453
454         //banking functions
455         int set_bank (uint32_t bank_start, lo_message msg);
456         int _set_bank (uint32_t bank_start, lo_address addr);
457         int bank_up (lo_message msg);
458         int bank_down (lo_message msg);
459         int set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gmode, lo_message msg);
460         int set_surface_bank_size (uint32_t bs, lo_message msg);
461         int set_surface_strip_types (uint32_t st, lo_message msg);
462         int set_surface_feedback (uint32_t fb, lo_message msg);
463         int set_surface_gainmode (uint32_t gm, lo_message msg);
464
465         int master_set_gain (float dB);
466         int master_set_fader (uint32_t position);
467         int master_set_trim (float dB);
468         int master_set_pan_stereo_position (float position, lo_message msg);
469         int master_set_mute (uint32_t state);
470         int monitor_set_gain (float dB);
471         int monitor_set_fader (uint32_t position);
472         int sel_recenable (uint32_t state, lo_message msg);
473         int sel_recsafe (uint32_t state, lo_message msg);
474         int sel_mute (uint32_t state, lo_message msg);
475         int sel_solo (uint32_t state, lo_message msg);
476         int sel_solo_iso (uint32_t state, lo_message msg);
477         int sel_solo_safe (uint32_t state, lo_message msg);
478         int sel_monitor_input (uint32_t state, lo_message msg);
479         int sel_monitor_disk (uint32_t state, lo_message msg);
480         int sel_phase (uint32_t state, lo_message msg);
481         int sel_gain (float state, lo_message msg);
482         int sel_fader (float state, lo_message msg);
483         int sel_trim (float val, lo_message msg);
484         int sel_pan_position (float val, lo_message msg);
485         int sel_pan_width (float val, lo_message msg);
486         int sel_sendgain (int id, float dB, lo_message msg);
487         int sel_sendfader (int id, float pos, lo_message msg);
488         int sel_sendenable (int id, float pos, lo_message msg);
489
490         void listen_to_route (boost::shared_ptr<ARDOUR::Stripable>, lo_address);
491         void end_listen (boost::shared_ptr<ARDOUR::Stripable>, lo_address);
492         void drop_route (boost::weak_ptr<ARDOUR::Stripable>);
493         void route_lost (boost::weak_ptr<ARDOUR::Stripable>);
494         void gui_selection_changed (ARDOUR::StripableNotificationListPtr stripables);
495
496         void route_name_changed (const PBD::PropertyChange&, boost::weak_ptr<ARDOUR::Route> r, lo_address addr);
497         void recalcbanks ();
498         void _recalcbanks ();
499         void notify_routes_added (ARDOUR::RouteList &);
500         void notify_vca_added (ARDOUR::VCAList &);
501
502         void update_clock ();
503         bool periodic (void);
504         sigc::connection periodic_connection;
505         PBD::ScopedConnectionList session_connections;
506
507         int route_send_fail (std::string path, uint32_t ssid, float val, lo_address addr);
508         int sel_send_fail (std::string path, uint32_t id, float val, lo_address addr);
509
510         typedef std::list<OSCRouteObserver*> RouteObservers;
511
512         RouteObservers route_observers;
513
514         typedef std::list<OSCGlobalObserver*> GlobalObservers;
515         GlobalObservers global_observers;
516
517         void debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc);
518
519         static OSC* _instance;
520
521         mutable void *gui;
522         void build_gui ();
523 };
524
525 } // namespace
526
527 #endif // ardour_osc_h