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