OSC API update for surfaces that always send float parameters.
[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         int set_active (bool yn);
73         bool get_active () const;
74         int set_feedback (bool yn);
75         bool get_feedback () const;
76
77         void set_namespace_root (std::string);
78
79         int start ();
80         int stop ();
81
82         static void* request_factory (uint32_t);
83
84   protected:
85         void thread_init ();
86         void do_request (OSCUIRequest*);
87
88         GSource* local_server;
89         GSource* remote_server;
90
91         bool osc_input_handler (Glib::IOCondition, lo_server);
92
93   private:
94         uint32_t _port;
95         volatile bool _ok;
96         volatile bool _shutdown;
97         lo_server _osc_server;
98         lo_server _osc_unix_server;
99         std::string _osc_unix_socket_path;
100         std::string _osc_url_file;
101         std::string _namespace_root;
102         bool _send_route_changes;
103
104         void register_callbacks ();
105
106         void route_added (ARDOUR::RouteList&);
107
108         // Handlers for "Application Hook" signals
109         void session_loaded (ARDOUR::Session&);
110         void session_exported (std::string, std::string);
111
112         // end "Application Hook" handles
113
114         std::string get_server_url ();
115         std::string get_unix_server_url ();
116
117         void send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg);
118         void current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg);
119
120         int current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
121
122         int catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data);
123         static int _catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
124
125         void routes_list (lo_message msg);
126         void transport_frame (lo_message msg);
127         void transport_speed (lo_message msg);
128         void record_enabled (lo_message msg);
129
130 #define PATH_CALLBACK_MSG(name)                                 \
131         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
132                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
133         } \
134         int cb_ ## name (const char *, const char *, lo_arg **, int, void *data) { \
135                 name (data);            \
136                 return 0;               \
137         }
138
139         PATH_CALLBACK_MSG(routes_list);
140         PATH_CALLBACK_MSG(transport_frame);
141         PATH_CALLBACK_MSG(transport_speed);
142         PATH_CALLBACK_MSG(record_enabled);
143
144 #define PATH_CALLBACK(name) \
145         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
146                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
147         } \
148         int cb_ ## name (const char *, const char *types, lo_arg ** argv, int argc, void *) { \
149                 if (argc > 0 && !strcmp (types, "f") && argv[0]->f != 1.0) { return 0; } \
150                 name (); \
151                 return 0; \
152         }
153
154         PATH_CALLBACK(add_marker);
155         PATH_CALLBACK(loop_toggle);
156         PATH_CALLBACK(goto_start);
157         PATH_CALLBACK(goto_end);
158         PATH_CALLBACK(rewind);
159         PATH_CALLBACK(ffwd);
160         PATH_CALLBACK(transport_stop);
161         PATH_CALLBACK(transport_play);
162         PATH_CALLBACK(save_state);
163         PATH_CALLBACK(prev_marker);
164         PATH_CALLBACK(next_marker);
165         PATH_CALLBACK(undo);
166         PATH_CALLBACK(redo);
167         PATH_CALLBACK(toggle_punch_in);
168         PATH_CALLBACK(toggle_punch_out);
169         PATH_CALLBACK(rec_enable_toggle);
170         PATH_CALLBACK(toggle_all_rec_enables);
171
172 #define PATH_CALLBACK1(name,type,optional)                                      \
173         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
174                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
175         } \
176         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
177                 if (argc > 0) {                                         \
178                         name (optional argv[0]->type);          \
179                 }                                                       \
180                 return 0;                                               \
181         }
182
183         PATH_CALLBACK1(set_transport_speed,f,);
184         PATH_CALLBACK1(access_action,s,&);
185
186 #define PATH_CALLBACK2(name,arg1type,arg2type)                  \
187         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
188                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
189         } \
190         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
191                 if (argc > 1) {                                         \
192                         name (argv[0]->arg1type, argv[1]->arg2type); \
193                 }                                                       \
194                 return 0;                                               \
195         }
196
197 #define PATH_CALLBACK3(name,arg1type,arg2type,arg3type)                \
198         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
199                return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
200         } \
201         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
202                 if (argc > 1) {                                                \
203                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type); \
204                 }                                                      \
205                return 0;                                               \
206        }
207
208 #define PATH_CALLBACK4(name,arg1type,arg2type,arg3type,arg4type)               \
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 *, const char *, lo_arg **argv, int argc, void *) { \
213                 if (argc > 1) {                                                \
214                  name (argv[0]->arg1type, argv[1]->arg2type,argv[2]->arg3type,argv[3]->arg4type); \
215                 }                                                      \
216                return 0;                                               \
217        }
218
219         PATH_CALLBACK2(locate,i,i);
220         PATH_CALLBACK2(loop_location,i,i);
221         PATH_CALLBACK2(route_mute,i,i);
222         PATH_CALLBACK2(route_solo,i,i);
223         PATH_CALLBACK2(route_recenable,i,i);
224         PATH_CALLBACK2(route_set_gain_abs,i,f);
225         PATH_CALLBACK2(route_set_gain_dB,i,f);
226         PATH_CALLBACK2(route_set_trim_abs,i,f);
227         PATH_CALLBACK2(route_set_trim_dB,i,f);
228         PATH_CALLBACK2(route_set_pan_stereo_position,i,f);
229         PATH_CALLBACK2(route_set_pan_stereo_width,i,f);
230         PATH_CALLBACK3(route_set_send_gain_abs,i,i,f);
231         PATH_CALLBACK3(route_set_send_gain_dB,i,i,f);
232         PATH_CALLBACK4(route_plugin_parameter,i,i,i,f);
233         PATH_CALLBACK3(route_plugin_parameter_print,i,i,i);
234
235         int route_mute (int rid, int yn);
236         int route_solo (int rid, int yn);
237         int route_recenable (int rid, int yn);
238         int route_set_gain_abs (int rid, float level);
239         int route_set_gain_dB (int rid, float dB);
240         int route_set_trim_abs (int rid, float level);
241         int route_set_trim_dB (int rid, float dB);
242         int route_set_pan_stereo_position (int rid, float left_right_fraction);
243         int route_set_pan_stereo_width (int rid, float percent);
244         int route_set_send_gain_abs (int rid, int sid, float val);
245         int route_set_send_gain_dB (int rid, int sid, float val);
246         int route_plugin_parameter (int rid, int piid,int par, float val);
247         int route_plugin_parameter_print (int rid, int piid,int par);
248
249         void listen_to_route (boost::shared_ptr<ARDOUR::Route>, lo_address);
250         void end_listen (boost::shared_ptr<ARDOUR::Route>, lo_address);
251         void drop_route (boost::weak_ptr<ARDOUR::Route>);
252
253         void route_name_changed (const PBD::PropertyChange&, boost::weak_ptr<ARDOUR::Route> r, lo_address addr);
254
255         void update_clock ();
256
257
258         typedef std::list<OSCRouteObserver*> RouteObservers;
259
260         RouteObservers route_observers;
261
262         static OSC* _instance;
263 };
264
265 } // namespace
266
267 #endif // ardour_osc_h