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