4d277fb8220b8f679d29832f666053dc17e5739c
[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
171         void register_callbacks ();
172
173         void route_added (ARDOUR::RouteList&);
174
175         // Handlers for "Application Hook" signals
176         void session_loaded (ARDOUR::Session&);
177         void session_exported (std::string, std::string);
178
179         // end "Application Hook" handles
180
181         std::string get_unix_server_url ();
182         OSCSurface * get_surface (lo_address addr);
183         uint32_t get_sid (uint32_t rid, lo_address addr);
184         uint32_t get_rid (uint32_t sid, lo_address addr);
185         void global_feedback (std::bitset<32> feedback, lo_address msg, uint32_t gainmode);
186
187         void send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg);
188         void current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg);
189
190         int current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
191
192         int catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data);
193         static int _catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
194
195         void routes_list (lo_message msg);
196         void transport_frame (lo_message msg);
197         void transport_speed (lo_message msg);
198         void record_enabled (lo_message msg);
199
200 #define OSC_DEBUG \
201         if (_debugmode == All) { \
202                 debugmsg (dgettext(PACKAGE, "OSC"), path, types, argv, argc); \
203         }
204
205 #define PATH_CALLBACK_MSG(name)                                 \
206         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
207                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
208         } \
209         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
210                 OSC_DEBUG;              \
211                 if (argc > 0 && !strcmp (types, "f") && argv[0]->f != 1.0) { return 0; } \
212                 name (data);            \
213                 return 0;               \
214         }
215
216         PATH_CALLBACK_MSG(routes_list);
217         PATH_CALLBACK_MSG(transport_frame);
218         PATH_CALLBACK_MSG(transport_speed);
219         PATH_CALLBACK_MSG(record_enabled);
220         PATH_CALLBACK_MSG(bank_up);
221         PATH_CALLBACK_MSG(bank_down);
222
223 #define PATH_CALLBACK(name) \
224         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
225                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
226         } \
227         int cb_ ## name (const char *path, const char *types, lo_arg ** argv, int argc, void *) { \
228                 OSC_DEBUG;              \
229                 if (argc > 0 && !strcmp (types, "f") && argv[0]->f != 1.0) { return 0; } \
230                 name (); \
231                 return 0; \
232         }
233
234         PATH_CALLBACK(add_marker);
235         PATH_CALLBACK(loop_toggle);
236         PATH_CALLBACK(goto_start);
237         PATH_CALLBACK(goto_end);
238         PATH_CALLBACK(rewind);
239         PATH_CALLBACK(ffwd);
240         PATH_CALLBACK(transport_stop);
241         PATH_CALLBACK(transport_play);
242         PATH_CALLBACK(save_state);
243         PATH_CALLBACK(prev_marker);
244         PATH_CALLBACK(next_marker);
245         PATH_CALLBACK(undo);
246         PATH_CALLBACK(redo);
247         PATH_CALLBACK(toggle_punch_in);
248         PATH_CALLBACK(toggle_punch_out);
249         PATH_CALLBACK(rec_enable_toggle);
250         PATH_CALLBACK(toggle_all_rec_enables);
251         PATH_CALLBACK(all_tracks_rec_in);
252         PATH_CALLBACK(all_tracks_rec_out);
253         PATH_CALLBACK(remove_marker_at_playhead);
254         PATH_CALLBACK(mark_in);
255         PATH_CALLBACK(mark_out);
256         PATH_CALLBACK(toggle_click);
257         PATH_CALLBACK(midi_panic);
258         PATH_CALLBACK(toggle_roll);
259         PATH_CALLBACK(stop_forget);
260         PATH_CALLBACK(set_punch_range);
261         PATH_CALLBACK(set_loop_range);
262         PATH_CALLBACK(set_session_range);
263         PATH_CALLBACK(toggle_monitor_mute);
264         PATH_CALLBACK(toggle_monitor_dim);
265         PATH_CALLBACK(toggle_monitor_mono);
266         PATH_CALLBACK(quick_snapshot_stay);
267         PATH_CALLBACK(quick_snapshot_switch);
268         PATH_CALLBACK(fit_1_track);
269         PATH_CALLBACK(fit_2_tracks);
270         PATH_CALLBACK(fit_4_tracks);
271         PATH_CALLBACK(fit_8_tracks);
272         PATH_CALLBACK(fit_16_tracks);
273         PATH_CALLBACK(fit_32_tracks);
274         PATH_CALLBACK(fit_all_tracks);
275         PATH_CALLBACK(zoom_100_ms);
276         PATH_CALLBACK(zoom_1_sec);
277         PATH_CALLBACK(zoom_10_sec);
278         PATH_CALLBACK(zoom_1_min);
279         PATH_CALLBACK(zoom_5_min);
280         PATH_CALLBACK(zoom_10_min);
281         PATH_CALLBACK(zoom_to_session);
282         PATH_CALLBACK(temporal_zoom_in);
283         PATH_CALLBACK(temporal_zoom_out);
284         PATH_CALLBACK(scroll_up_1_track);
285         PATH_CALLBACK(scroll_dn_1_track);
286         PATH_CALLBACK(scroll_up_1_page);
287         PATH_CALLBACK(scroll_dn_1_page);
288
289 #define PATH_CALLBACK1(name,type,optional)                                      \
290         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
291                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
292         } \
293         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *) { \
294                 OSC_DEBUG;              \
295                 if (argc > 0) {                                         \
296                         name (optional argv[0]->type);          \
297                 }                                                       \
298                 return 0;                                               \
299         }
300
301         PATH_CALLBACK1(set_transport_speed,f,);
302         PATH_CALLBACK1(access_action,s,&);
303
304         PATH_CALLBACK1(jump_by_bars,f,);
305         PATH_CALLBACK1(jump_by_seconds,f,);
306         PATH_CALLBACK1(master_set_gain,f,);
307         PATH_CALLBACK1(master_set_fader,i,);
308         PATH_CALLBACK1(master_set_trim,f,);
309         PATH_CALLBACK1(master_set_mute,i,);
310         PATH_CALLBACK1(monitor_set_gain,f,);
311         PATH_CALLBACK1(monitor_set_fader,i,);
312
313 #define PATH_CALLBACK1_MSG(name,arg1type)                       \
314         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
315                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
316         } \
317         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
318                 OSC_DEBUG;              \
319                 if (argc > 0) {                                         \
320                         name (argv[0]->arg1type, data); \
321                 }                                                       \
322                 return 0;                                               \
323         }
324
325         // pan position needs message info to send feedback
326         PATH_CALLBACK1_MSG(master_set_pan_stereo_position,f);
327
328         PATH_CALLBACK1_MSG(set_surface_bank_size,i);
329         PATH_CALLBACK1_MSG(set_surface_strip_types,i);
330         PATH_CALLBACK1_MSG(set_surface_feedback,i);
331         PATH_CALLBACK1_MSG(set_surface_gainmode,i);
332         PATH_CALLBACK1_MSG(sel_recenable,i);
333         PATH_CALLBACK1_MSG(sel_recsafe,i);
334         PATH_CALLBACK1_MSG(sel_mute,i);
335         PATH_CALLBACK1_MSG(sel_solo,i);
336         PATH_CALLBACK1_MSG(sel_monitor_input,i);
337         PATH_CALLBACK1_MSG(sel_monitor_disk,i);
338         PATH_CALLBACK1_MSG(sel_phase,i);
339         PATH_CALLBACK1_MSG(sel_gain,f);
340         PATH_CALLBACK1_MSG(sel_fader,f);
341         PATH_CALLBACK1_MSG(sel_trim,f);
342         PATH_CALLBACK1_MSG(sel_pan_position,f);
343         PATH_CALLBACK1_MSG(sel_pan_width,f);
344
345 #define PATH_CALLBACK2(name,arg1type,arg2type)                  \
346         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
347                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
348         } \
349         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *) { \
350                 OSC_DEBUG;              \
351                 if (argc > 1) {                                         \
352                         name (argv[0]->arg1type, argv[1]->arg2type); \
353                 }                                                       \
354                 return 0;                                               \
355         }
356
357 #define PATH_CALLBACK2_MSG(name,arg1type,arg2type)                      \
358         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
359                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
360         } \
361         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
362                 OSC_DEBUG;              \
363                 if (argc > 1) {                                         \
364                         name (argv[0]->arg1type, argv[1]->arg2type, data); \
365                 }                                                       \
366                 return 0;                                               \
367         }
368
369 #define PATH_CALLBACK3(name,arg1type,arg2type,arg3type)                \
370         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
371                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
372         } \
373         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
374                 OSC_DEBUG;              \
375                 if (argc > 1) {                                                \
376                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type, data); \
377                 }                                                      \
378                return 0;                                               \
379        }
380
381 #define PATH_CALLBACK4(name,arg1type,arg2type,arg3type,arg4type)               \
382         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
383                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
384         } \
385         int cb_ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data) { \
386                 OSC_DEBUG;              \
387                 if (argc > 1) {                                                \
388                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type,argv[3]->arg4type, data); \
389                 }                                                      \
390                return 0;                                               \
391        }
392
393         PATH_CALLBACK2_MSG(sel_sendgain,i,f);
394         PATH_CALLBACK2_MSG(sel_sendfader,i,f);
395
396         PATH_CALLBACK4(set_surface,i,i,i,i);
397         PATH_CALLBACK2(locate,i,i);
398         PATH_CALLBACK2(loop_location,i,i);
399         PATH_CALLBACK2_MSG(route_mute,i,i);
400         PATH_CALLBACK2_MSG(route_solo,i,i);
401         PATH_CALLBACK2_MSG(route_recenable,i,i);
402         PATH_CALLBACK2_MSG(route_recsafe,i,i);
403         PATH_CALLBACK2_MSG(route_monitor_input,i,i);
404         PATH_CALLBACK2_MSG(route_monitor_disk,i,i);
405         PATH_CALLBACK2_MSG(strip_phase,i,i);
406         PATH_CALLBACK2_MSG(strip_select,i,i);
407         PATH_CALLBACK2_MSG(strip_gui_select,i,i);
408         PATH_CALLBACK2_MSG(route_set_gain_abs,i,f);
409         PATH_CALLBACK2_MSG(route_set_gain_dB,i,f);
410         PATH_CALLBACK2_MSG(route_set_gain_fader,i,f);
411         PATH_CALLBACK2_MSG(route_set_trim_dB,i,f);
412         PATH_CALLBACK2_MSG(route_set_pan_stereo_position,i,f);
413         PATH_CALLBACK2_MSG(route_set_pan_stereo_width,i,f);
414         PATH_CALLBACK3(route_set_send_gain_abs,i,i,f);
415         PATH_CALLBACK3(route_set_send_gain_dB,i,i,f);
416         PATH_CALLBACK3(route_set_send_fader,i,i,f);
417         PATH_CALLBACK4(route_plugin_parameter,i,i,i,f);
418         PATH_CALLBACK3(route_plugin_parameter_print,i,i,i);
419
420         int route_mute (int rid, int yn, lo_message msg);
421         int route_solo (int rid, int yn, lo_message msg);
422         int route_recenable (int rid, int yn, lo_message msg);
423         int route_recsafe (int ssid, int yn, lo_message msg);
424         int route_monitor_input (int rid, int yn, lo_message msg);
425         int route_monitor_disk (int rid, int yn, lo_message msg);
426         int strip_phase (int rid, int yn, lo_message msg);
427         int strip_select (int rid, int yn, lo_message msg);
428         int _strip_select (int rid, lo_address addr);
429         int strip_gui_select (int rid, int yn, lo_message msg);
430         int route_set_gain_abs (int rid, float level, lo_message msg);
431         int route_set_gain_dB (int rid, float dB, lo_message msg);
432         int route_set_gain_fader (int rid, float pos, lo_message msg);
433         int route_set_trim_abs (int rid, float level, lo_message msg);
434         int route_set_trim_dB (int rid, float dB, lo_message msg);
435         int route_set_pan_stereo_position (int rid, float left_right_fraction, lo_message msg);
436         int route_set_pan_stereo_width (int rid, float percent, lo_message msg);
437         int route_set_send_gain_abs (int rid, int sid, float val, lo_message msg);
438         int route_set_send_gain_dB (int rid, int sid, float val, lo_message msg);
439         int route_set_send_fader (int rid, int sid, float val, lo_message msg);
440         int route_plugin_parameter (int rid, int piid,int par, float val, lo_message msg);
441         int route_plugin_parameter_print (int rid, int piid,int par, lo_message msg);
442
443         //banking functions
444         int set_bank (uint32_t bank_start, lo_message msg);
445         int bank_up (lo_message msg);
446         int bank_down (lo_message msg);
447         int set_surface (uint32_t b_size, uint32_t strips, uint32_t fb, uint32_t gmode, lo_message msg);
448         int set_surface_bank_size (uint32_t bs, lo_message msg);
449         int set_surface_strip_types (uint32_t st, lo_message msg);
450         int set_surface_feedback (uint32_t fb, lo_message msg);
451         int set_surface_gainmode (uint32_t gm, lo_message msg);
452
453         int master_set_gain (float dB);
454         int master_set_fader (uint32_t position);
455         int master_set_trim (float dB);
456         int master_set_pan_stereo_position (float position, lo_message msg);
457         int master_set_mute (uint32_t state);
458         int monitor_set_gain (float dB);
459         int monitor_set_fader (uint32_t position);
460         int sel_recenable (uint32_t state, lo_message msg);
461         int sel_recsafe (uint32_t state, lo_message msg);
462         int sel_mute (uint32_t state, lo_message msg);
463         int sel_solo (uint32_t state, lo_message msg);
464         int sel_monitor_input (uint32_t state, lo_message msg);
465         int sel_monitor_disk (uint32_t state, lo_message msg);
466         int sel_phase (uint32_t state, lo_message msg);
467         int sel_gain (float state, lo_message msg);
468         int sel_fader (float state, lo_message msg);
469         int sel_trim (float val, lo_message msg);
470         int sel_pan_position (float val, lo_message msg);
471         int sel_pan_width (float val, lo_message msg);
472         int sel_sendgain (int id, float dB, lo_message msg);
473         int sel_sendfader (int id, float pos, lo_message msg);
474
475         void listen_to_route (boost::shared_ptr<ARDOUR::Stripable>, lo_address);
476         void end_listen (boost::shared_ptr<ARDOUR::Stripable>, lo_address);
477         void drop_route (boost::weak_ptr<ARDOUR::Stripable>);
478         void gui_selection_changed (ARDOUR::StripableNotificationListPtr stripables);
479
480         void route_name_changed (const PBD::PropertyChange&, boost::weak_ptr<ARDOUR::Route> r, lo_address addr);
481
482         void update_clock ();
483         bool periodic (void);
484         sigc::connection periodic_connection;
485         PBD::ScopedConnectionList session_connections;
486
487         int route_send_fail (std::string path, uint32_t ssid, float val, lo_address addr);
488
489         typedef std::list<OSCRouteObserver*> RouteObservers;
490
491         RouteObservers route_observers;
492
493         typedef std::list<OSCGlobalObserver*> GlobalObservers;
494         GlobalObservers global_observers;
495
496         void debugmsg (const char *prefix, const char *path, const char* types, lo_arg **argv, int argc);
497
498         static OSC* _instance;
499
500         mutable void *gui;
501         void build_gui ();
502 };
503
504 } // namespace
505
506 #endif // ardour_osc_h