Merge branch 'master' of git@git.ardour.org:ardour/ardour
[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
25 #include <sys/time.h>
26 #include <pthread.h>
27
28 #include <boost/shared_ptr.hpp>
29
30 #include <lo/lo.h>
31
32 #include <glibmm/main.h>
33
34 #include "pbd/abstract_ui.h"
35
36 #include "ardour/types.h"
37 #include "control_protocol/control_protocol.h"
38
39 class OSCControllable;
40 class OSCRouteObserver;
41
42 namespace ARDOUR {
43 class Session;
44 class Route;
45 }
46         
47 /* this is mostly a placeholder because I suspect that at some
48    point we will want to add more members to accomodate
49    certain types of requests to the OSC UI
50 */
51
52 struct OSCUIRequest : public BaseUI::BaseRequestObject {
53   public:
54         OSCUIRequest () {}
55         ~OSCUIRequest() {}
56 };
57
58 class OSC : public ARDOUR::ControlProtocol, public AbstractUI<OSCUIRequest>
59 {
60   public:
61         OSC (ARDOUR::Session&, uint32_t port);
62         virtual ~OSC();
63
64         static OSC* instance() { return _instance; }
65
66         XMLNode& get_state ();
67         int set_state (const XMLNode&, int version);
68
69         int set_active (bool yn);
70         bool get_active () const;
71         int set_feedback (bool yn);
72         bool get_feedback () const;
73
74         void set_namespace_root (std::string);
75
76         int start ();
77         int stop ();
78
79   protected:
80         void thread_init ();
81         void do_request (OSCUIRequest*);
82
83         GSource* local_server;
84         GSource* remote_server;
85         
86         bool osc_input_handler (Glib::IOCondition, lo_server);
87
88   private:
89         uint32_t _port;
90         volatile bool _ok;
91         volatile bool _shutdown;
92         lo_server _osc_server;
93         lo_server _osc_unix_server;
94         std::string _osc_unix_socket_path;
95         std::string _osc_url_file;
96         std::string _namespace_root;
97         bool _send_route_changes;
98
99         void register_callbacks ();
100
101         void route_added (ARDOUR::RouteList&);
102                 
103         // Handlers for "Application Hook" signals
104         void session_loaded (ARDOUR::Session&);
105         void session_exported (std::string, std::string);
106
107         // end "Application Hook" handles
108
109         std::string get_server_url ();
110         std::string get_unix_server_url ();
111
112         void send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg);
113         void current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg);
114         
115         int current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
116         
117         int catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data);
118         static int _catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
119
120         void routes_list (lo_message msg);
121         void transport_frame(lo_message msg);
122
123 #define PATH_CALLBACK_MSG(name)                                 \
124         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
125                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
126         } \
127         int cb_ ## name (const char *, const char *, lo_arg **, int, void *data) { \
128                 name (data);            \
129                 return 0;               \
130         }
131         
132         PATH_CALLBACK_MSG(routes_list);
133         PATH_CALLBACK_MSG(transport_frame);
134         
135 #define PATH_CALLBACK(name) \
136         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
137                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
138         } \
139         int cb_ ## name (const char *, const char *, lo_arg **, int, void *) { \
140                 name (); \
141                 return 0; \
142         }
143
144         PATH_CALLBACK(add_marker);
145         PATH_CALLBACK(loop_toggle);
146         PATH_CALLBACK(goto_start);
147         PATH_CALLBACK(goto_end);
148         PATH_CALLBACK(rewind);
149         PATH_CALLBACK(ffwd);
150         PATH_CALLBACK(transport_stop);
151         PATH_CALLBACK(transport_play);
152         PATH_CALLBACK(save_state);
153         PATH_CALLBACK(prev_marker);
154         PATH_CALLBACK(next_marker);
155         PATH_CALLBACK(undo);
156         PATH_CALLBACK(redo);
157         PATH_CALLBACK(toggle_punch_in);
158         PATH_CALLBACK(toggle_punch_out);
159         PATH_CALLBACK(rec_enable_toggle);
160         PATH_CALLBACK(toggle_all_rec_enables);
161
162 #define PATH_CALLBACK1(name,type,optional)                                      \
163         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
164                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
165         } \
166         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
167                 if (argc > 0) {                                         \
168                         name (optional argv[0]->type);          \
169                 }                                                       \
170                 return 0;                                               \
171         }
172
173         PATH_CALLBACK1(set_transport_speed,f,);
174         PATH_CALLBACK1(access_action,s,&);
175
176 #define PATH_CALLBACK2(name,arg1type,arg2type)                  \
177         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
178                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
179         } \
180         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
181                 if (argc > 1) {                                         \
182                         name (argv[0]->arg1type, argv[1]->arg2type); \
183                 }                                                       \
184                 return 0;                                               \
185         }
186
187 #define PATH_CALLBACK3(name,arg1type,arg2type,arg3type)                \
188         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
189                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
190         } \
191         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *data) { \
192                 if (argc > 1) {                                                \
193                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type); \
194                 }                                                      \
195                return 0;                                               \
196        }
197
198 #define PATH_CALLBACK4(name,arg1type,arg2type,arg3type,arg4type)               \
199         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
200                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
201         } \
202         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *data) { \
203                 if (argc > 1) {                                                \
204                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type,argv[3]->arg4type); \
205                 }                                                      \
206                return 0;                                               \
207        } 
208
209         PATH_CALLBACK2(locate,i,i);
210         PATH_CALLBACK2(route_mute,i,i);
211         PATH_CALLBACK2(route_solo,i,i);
212         PATH_CALLBACK2(route_recenable,i,i);
213         PATH_CALLBACK2(route_set_gain_abs,i,f);
214         PATH_CALLBACK2(route_set_gain_dB,i,f);
215         PATH_CALLBACK2(route_set_pan_stereo_position,i,f);
216         PATH_CALLBACK2(route_set_pan_stereo_width,i,f);
217         PATH_CALLBACK3(route_set_send_gain_abs,i,i,f);
218         PATH_CALLBACK3(route_set_send_gain_dB,i,i,f);
219         PATH_CALLBACK4(route_plugin_parameter,i,i,i,f);
220         PATH_CALLBACK3(route_plugin_parameter_print,i,i,i); 
221
222         int route_mute (int rid, int yn);
223         int route_solo (int rid, int yn);
224         int route_recenable (int rid, int yn);
225         int route_set_gain_abs (int rid, float level);
226         int route_set_gain_dB (int rid, float dB);
227         int route_set_pan_stereo_position (int rid, float left_right_fraction);
228         int route_set_pan_stereo_width (int rid, float percent);
229         int route_set_send_gain_abs (int rid, int sid, float val);
230         int route_set_send_gain_dB (int rid, int sid, float val);
231         int route_plugin_parameter (int rid, int piid,int par, float val);
232         int route_plugin_parameter_print (int rid, int piid,int par);
233         
234         void listen_to_route (boost::shared_ptr<ARDOUR::Route>, lo_address);
235         void end_listen (boost::shared_ptr<ARDOUR::Route>, lo_address);
236         void drop_route (boost::weak_ptr<ARDOUR::Route>);
237         
238         void route_name_changed (const PBD::PropertyChange&, boost::weak_ptr<ARDOUR::Route> r, lo_address addr);
239         
240         void update_clock ();
241
242
243         typedef std::list<OSCRouteObserver*> RouteObservers;
244         
245         RouteObservers route_observers;
246
247         static OSC* _instance;
248 };
249
250 #endif // ardour_osc_h