Assists music production by grouping standalone programs into sessions. Community version of "Non Session Manager".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
12KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2010 Jonathan Moore Liles */
  3. /* */
  4. /* This program is free software; you can redistribute it and/or modify it */
  5. /* under the terms of the GNU General Public License as published by the */
  6. /* Free Software Foundation; either version 2 of the License, or (at your */
  7. /* option) any later version. */
  8. /* */
  9. /* This program is distributed in the hope that it will be useful, but WITHOUT */
  10. /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
  11. /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
  12. /* more details. */
  13. /* */
  14. /* You should have received a copy of the GNU General Public License along */
  15. /* with This program; see the file COPYING. If not,write to the Free Software */
  16. /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /*******************************************************************************/
  18. #pragma once
  19. #include <lo/lo.h>
  20. #include "Thread.H"
  21. #include <list>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. namespace OSC
  25. {
  26. class OSC_Value
  27. {
  28. protected:
  29. char _type;
  30. float f;
  31. double d;
  32. int i;
  33. const char *s;
  34. public:
  35. OSC_Value ( const OSC_Value &rhs )
  36. {
  37. _type = rhs._type;
  38. f =rhs.f;
  39. d = rhs.d;
  40. i = rhs.i;
  41. s = rhs.s;
  42. }
  43. OSC_Value ( )
  44. {
  45. _type = 0;
  46. f = 0;
  47. d = 0;
  48. i = 0;
  49. s = 0;
  50. }
  51. virtual ~OSC_Value ( ) { }
  52. virtual char type ( void ) const { return _type; }
  53. };
  54. class OSC_Float : public OSC_Value
  55. {
  56. public:
  57. float value ( void ) const { return f; }
  58. OSC_Float ( float v )
  59. {
  60. _type = 'f';
  61. f = v;
  62. }
  63. };
  64. class OSC_Int : public OSC_Value
  65. {
  66. public:
  67. int value ( void ) const { return i; }
  68. OSC_Int ( int v )
  69. {
  70. _type = 'i';
  71. i = v;
  72. }
  73. };
  74. class OSC_String : public OSC_Value
  75. {
  76. public:
  77. const char * value ( void ) const { return s; }
  78. OSC_String ( const char *v )
  79. {
  80. _type = 's';
  81. s = v;
  82. }
  83. };
  84. struct Parameter_Limits
  85. {
  86. float min;
  87. float max;
  88. float default_value;
  89. };
  90. class Endpoint;
  91. class Signal;
  92. struct Peer
  93. {
  94. bool _scanning;
  95. char *name;
  96. lo_address addr;
  97. std::list<Signal*> _signals;
  98. };
  99. struct Target
  100. {
  101. Peer *peer;
  102. int signal_id;
  103. float value;
  104. };
  105. typedef int (*signal_handler) ( float value, void *user_data );
  106. class Signal
  107. {
  108. static int next_id;
  109. public:
  110. enum Direction {
  111. Input,
  112. Output,
  113. Bidirectional
  114. };
  115. private:
  116. Endpoint *_endpoint;
  117. Peer *_peer;
  118. int _id;
  119. char *_path;
  120. char *_documentation;
  121. float _value;
  122. std::list<Target*> _outgoing;
  123. Direction _direction;
  124. /* FIXME:
  125. In order to support signal mixing, the receiver must track
  126. each connected signal channel separately, and add the
  127. values together before invoking the handler.
  128. */
  129. std::list<Target*> _incoming;
  130. signal_handler _handler;
  131. void *_user_data;
  132. public:
  133. Parameter_Limits _parameter_limits;
  134. Signal ( const char *path, Direction dir )
  135. {
  136. _direction = dir;
  137. _path = strdup( path );
  138. _id = ++next_id;
  139. _value = 0.0f;
  140. _endpoint = NULL;
  141. _peer = NULL;
  142. }
  143. ~Signal ( );
  144. static Signal *get_peer_signal_by_id ( Peer *p, int signal_id );
  145. bool connected ( void ) { return _outgoing.size() + _incoming.size(); }
  146. void get_connected_peer_name_and_path ( char **peer_name, char **path );
  147. int id ( void ) const { return _id; }
  148. Direction direction ( void ) { return _direction; }
  149. void parameter_limits ( float min, float max, float default_value )
  150. {
  151. _parameter_limits.min = min;
  152. _parameter_limits.max = max;
  153. _parameter_limits.default_value = default_value;
  154. _value = default_value;
  155. }
  156. Parameter_Limits& parameter_limits ( void ) { return _parameter_limits; }
  157. const char *path ( void ) const { return _path; }
  158. void rename ( const char *name );
  159. /* publishes value to targets */
  160. void value ( float v );
  161. /* get current value */
  162. float value ( void ) const { return _value; }
  163. bool is_connected_to ( const Signal *s ) const;
  164. friend class Endpoint;
  165. };
  166. class Method
  167. {
  168. char *_path;
  169. char *_typespec;
  170. char *_documentation;
  171. struct Parameter_Limits *_parameter_limits;
  172. public:
  173. const char *path ( void ) { return _path; }
  174. const char *typespec ( void ) { return _typespec; }
  175. Method ( );
  176. ~Method ( );
  177. friend class Endpoint;
  178. };
  179. class Endpoint
  180. {
  181. static void error_handler(int num, const char *msg, const char *path);
  182. static int osc_reply ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  183. static int osc_signal_lister ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  184. static int osc_generic ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  185. static int osc_sig_handler ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  186. static int osc_sig_renamed ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  187. static int osc_sig_disconnect ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  188. static int osc_sig_connect ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  189. static int osc_sig_hello ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  190. void scan_peer ( const char *name, const char *url );
  191. Thread _thread;
  192. // lo_server_thread _st;
  193. lo_server _server;
  194. std::list<Peer*> _peers;
  195. std::list<Signal*> _signals;
  196. std::list<Method*> _methods;
  197. static void *osc_thread ( void *arg );
  198. void osc_thread ( void );
  199. OSC::Signal *find_signal_by_id ( int id );
  200. OSC::Signal *find_peer_signal_by_path ( Peer *p, const char *path );
  201. OSC::Signal *find_peer_signal_by_id ( Peer *p, int id );
  202. Peer *find_peer_by_name ( const char *name );
  203. Peer *find_peer_by_address ( lo_address addr );
  204. static bool address_matches ( lo_address addr1, lo_address addr2 );
  205. static Target *find_target_by_peer_address ( std::list<Target*> *l, lo_address addr );
  206. char *_name;
  207. public:
  208. void list_peers ( void (*callback) (const char *, const OSC::Signal *, void * ), void *v );
  209. int init ( int proto, const char *port = 0 );
  210. Endpoint ( );
  211. ~Endpoint ( );
  212. bool disconnect_signal( OSC::Signal *s, const char *peer_name, const char *signal_path );
  213. bool connect_signal( OSC::Signal *s, const char *peer_name, const char *signal_path );
  214. bool connect_signal( OSC::Signal *s, const char *peer_name, int signal_id );
  215. Signal *add_signal ( const char *path, Signal::Direction dir, signal_handler handler, void *user_data );
  216. Method *add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data, const char *argument_description );
  217. void del_method ( const char *path, const char *typespec );
  218. void del_method ( Method* method );
  219. void del_signal ( Signal *signal );
  220. void start ( void );
  221. void stop ( void );
  222. int port ( void ) const;
  223. char * url ( void ) const;
  224. void check ( void ) const;
  225. void wait ( int timeout ) const;
  226. void run ( void ) const;
  227. void name ( const char *name ) { _name = strdup( name ); }
  228. const char *name ( void ) { return _name; }
  229. void hello ( const char *url );
  230. int send ( lo_address to, const char *path, std::list< OSC_Value > values );
  231. /* overloads for common message formats */
  232. int send ( lo_address to, const char *path );
  233. int send ( lo_address to, const char *path, float v );
  234. int send ( lo_address to, const char *path, double v );
  235. int send ( lo_address to, const char *path, int v );
  236. int send ( lo_address to, const char *path, long v );
  237. int send ( lo_address to, const char *path, int v1, int v2 );
  238. int send ( lo_address to, const char *path, int v1, float v2 );
  239. int send ( lo_address to, const char *path, const char *v );
  240. int send ( lo_address to, const char *path, const char *v1, float v2 );
  241. int send ( lo_address to, const char *path, const char *v1, int v2, int v3 );
  242. int send ( lo_address to, const char *path, const char *v1, const char *v2 );
  243. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3 );
  244. int send ( lo_address to, const char *path, const char *v1, int v2, int v3, int v4 );
  245. int send ( lo_address to, const char *path, const char *v1, const char *v2, int v3, int v4, int v5 );
  246. int send ( lo_address to, const char *path, const char *v1, int v2 );
  247. int send ( lo_address to, const char *path, int v1, const char *v2 );
  248. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, int v4, int v5, int v6 );
  249. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3 );
  250. int send ( lo_address to, const char *path, int v1, const char *v2, const char *v3, const char *v4 );
  251. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3, const char *v4, const char *v5 );
  252. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4, const char *v5 );
  253. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4 );
  254. int send ( lo_address to, const char *path, lo_message msg );
  255. int send ( lo_address to, const char *path, const char *v1, const char *v2, int v3, float v4, float v5, float v6 );
  256. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, int v4, float v5, float v6, float v7 );
  257. // can be used to point back to owning object.
  258. void *owner;
  259. };
  260. };
  261. /* helper macros for defining OSC handlers */
  262. #define OSC_NAME( name ) osc_ ## name
  263. #define OSC_DMSG() DMESSAGE( "Got OSC message: %s", path );
  264. #define OSC_HANDLER( name ) static int OSC_NAME( name ) ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  265. #define OSC_REPLY_OK() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "ok" )
  266. #define OSC_REPLY( value ) ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, value )
  267. #define OSC_REPLY_ERR() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "err" )
  268. #define OSC_ENDPOINT() ((OSC::Endpoint*)user_data)