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