switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[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 #include "pbd/scoped_connections.h"
36
37 #include "ardour/types.h"
38 #include "control_protocol/control_protocol.h"
39
40 class OSCControllable;
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 MIDI UI
50 */
51
52 struct OSCUIRequest : public BaseUI::BaseRequestObject {
53   public:
54         OSCUIRequest () {}
55         ~OSCUIRequest() {}
56 };
57
58 class OSC : public ARDOUR::ControlProtocol, public PBD::ScopedConnectionList, public AbstractUI<OSCUIRequest>
59 {
60   public:
61         OSC (ARDOUR::Session&, uint32_t port);
62         virtual ~OSC();
63
64         XMLNode& get_state ();
65         int set_state (const XMLNode&, int version);
66
67         int set_active (bool yn);
68         bool get_active () const;
69         int set_feedback (bool yn);
70         bool get_feedback () const;
71
72         void set_namespace_root (std::string);
73
74         int start ();
75         int stop ();
76
77   protected:
78         void thread_init ();
79         void do_request (OSCUIRequest*);
80
81         GSource* local_server;
82         GSource* remote_server;
83         
84         bool osc_input_handler (Glib::IOCondition, lo_server);
85
86   private:
87         uint32_t _port;
88         volatile bool _ok;
89         volatile bool _shutdown;
90         lo_server _osc_server;
91         lo_server _osc_unix_server;
92         std::string _osc_unix_socket_path;
93         std::string _osc_url_file;
94         std::string _namespace_root;
95         bool _send_route_changes;
96
97         void register_callbacks ();
98
99         void route_added (ARDOUR::RouteList&);
100                 
101         // Handlers for "Application Hook" signals
102         void session_loaded (ARDOUR::Session&);
103         void session_exported (std::string, std::string);
104
105         // end "Application Hook" handles
106
107         std::string get_server_url ();
108         std::string get_unix_server_url ();
109
110         void send_current_value (const char* path, lo_arg** argv, int argc, lo_message msg);
111         void current_value_query (const char* path, size_t len, lo_arg **argv, int argc, lo_message msg);
112         int catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data);
113         static int _catchall (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
114
115         int current_value (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data);
116
117 #define PATH_CALLBACK(name) \
118         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
119                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
120         } \
121         int cb_ ## name (const char *, const char *, lo_arg **, int, void *) { \
122                 name (); \
123                 return 0; \
124         }
125
126         PATH_CALLBACK(add_marker);
127         PATH_CALLBACK(loop_toggle);
128         PATH_CALLBACK(goto_start);
129         PATH_CALLBACK(goto_end);
130         PATH_CALLBACK(rewind);
131         PATH_CALLBACK(ffwd);
132         PATH_CALLBACK(transport_stop);
133         PATH_CALLBACK(transport_play);
134         PATH_CALLBACK(save_state);
135         PATH_CALLBACK(prev_marker);
136         PATH_CALLBACK(next_marker);
137         PATH_CALLBACK(undo);
138         PATH_CALLBACK(redo);
139         PATH_CALLBACK(toggle_punch_in);
140         PATH_CALLBACK(toggle_punch_out);
141         PATH_CALLBACK(rec_enable_toggle);
142         PATH_CALLBACK(toggle_all_rec_enables);
143
144 #define PATH_CALLBACK1(name,type,optional)                                      \
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 *, lo_arg **argv, int argc, void *) { \
149                 if (argc > 0) {                                         \
150                         name (optional argv[0]->type);          \
151                 }                                                       \
152                 return 0;                                               \
153         }
154
155         PATH_CALLBACK1(set_transport_speed,f,);
156         PATH_CALLBACK1(access_action,s,&);
157
158 #define PATH_CALLBACK2(name,arg1type,arg2type)                  \
159         static int _ ## name (const char *path, const char *types, lo_arg **argv, int argc, void *data, void *user_data) { \
160                 return static_cast<OSC*>(user_data)->cb_ ## name (path, types, argv, argc, data); \
161         } \
162         int cb_ ## name (const char *, const char *, lo_arg **argv, int argc, void *) { \
163                 if (argc > 1) {                                         \
164                         name (argv[0]->arg1type, argv[1]->arg2type); \
165                 }                                                       \
166                 return 0;                                               \
167         }
168
169         PATH_CALLBACK2(route_mute,i,i);
170         PATH_CALLBACK2(route_solo,i,i);
171         PATH_CALLBACK2(route_recenable,i,i);
172         PATH_CALLBACK2(route_set_gain_abs,i,f);
173         PATH_CALLBACK2(route_set_gain_dB,i,f);
174
175         int route_mute (int rid, int yn);
176         int route_solo (int rid, int yn);
177         int route_recenable (int rid, int yn);
178         int route_set_gain_abs (int rid, float level);
179         int route_set_gain_dB (int rid, float dB);
180
181         void listen_to_route (boost::shared_ptr<ARDOUR::Route>, lo_address);
182         void end_listen (boost::shared_ptr<ARDOUR::Route>, lo_address);
183         void drop_route (boost::weak_ptr<ARDOUR::Route>);
184
185         typedef std::list<OSCControllable*> Controllables;
186
187         Controllables controllables;
188 };
189
190 #endif // ardour_osc_h