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.

345 lines
11KB

  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. // char *path;
  103. int signal_id;
  104. float value;
  105. };
  106. typedef int (*signal_handler) ( float value, void *user_data );
  107. class Signal
  108. {
  109. static int next_id;
  110. public:
  111. enum Direction {
  112. Input,
  113. Output,
  114. Bidirectional
  115. };
  116. private:
  117. Endpoint *_endpoint;
  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. }
  141. ~Signal ( )
  142. {
  143. free( _path );
  144. }
  145. bool connected ( void ) { return _outgoing.size() + _incoming.size(); }
  146. int id ( void ) { return _id; }
  147. Direction direction ( void ) { return _direction; }
  148. void parameter_limits ( float min, float max, float default_value )
  149. {
  150. _parameter_limits.min = min;
  151. _parameter_limits.max = max;
  152. _parameter_limits.default_value = default_value;
  153. _value = default_value;
  154. }
  155. Parameter_Limits& parameter_limits ( void ) { return _parameter_limits; }
  156. const char *path ( void ) { return _path; }
  157. /* publishes value to targets */
  158. void value ( float v );
  159. /* get current value */
  160. float value ( void ) { return _value; }
  161. friend class Endpoint;
  162. };
  163. class Method
  164. {
  165. char *_path;
  166. char *_typespec;
  167. char *_documentation;
  168. struct Parameter_Limits *_parameter_limits;
  169. public:
  170. const char *path ( void ) { return _path; }
  171. const char *typespec ( void ) { return _typespec; }
  172. Method ( );
  173. ~Method ( );
  174. friend class Endpoint;
  175. };
  176. class Endpoint
  177. {
  178. static void error_handler(int num, const char *msg, const char *path);
  179. static int osc_reply ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  180. static int osc_signal_lister ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  181. static int osc_generic ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  182. static int osc_sig_handler ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  183. Thread _thread;
  184. // lo_server_thread _st;
  185. lo_server _server;
  186. std::list<Peer*> _peers;
  187. std::list<Signal*> _signals;
  188. std::list<Method*> _methods;
  189. static void *osc_thread ( void *arg );
  190. void osc_thread ( void );
  191. OSC::Signal *find_signal_by_id ( int id );
  192. Peer *find_peer_by_name ( const char *name );
  193. Peer *find_peer_by_address ( lo_address addr );
  194. static bool address_matches ( lo_address addr1, lo_address addr2 );
  195. static Target *find_target_by_peer_address ( std::list<Target*> *l, lo_address addr );
  196. public:
  197. void list_peers ( void (*callback) (const char *, const char *, int, void * ), void *v );
  198. int init ( const char *port = 0 );
  199. Endpoint ( );
  200. ~Endpoint ( );
  201. bool connect_signal( OSC::Signal *s, const char *peer_name, int signal_id );
  202. Signal *add_signal ( const char *path, Signal::Direction dir, signal_handler handler, void *user_data );
  203. Method *add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data, const char *argument_description );
  204. void del_method ( const char *path, const char *typespec );
  205. void del_method ( Method* method );
  206. void del_signal ( Signal *signal );
  207. void start ( void );
  208. void stop ( void );
  209. int port ( void ) const;
  210. char * url ( void ) const;
  211. void scan_peer ( const char *name, const char *url );
  212. void check ( void ) const;
  213. void wait ( int timeout ) const;
  214. void run ( void ) const;
  215. int send ( lo_address to, const char *path, std::list< OSC_Value > values );
  216. /* overloads for common message formats */
  217. int send ( lo_address to, const char *path );
  218. int send ( lo_address to, const char *path, float v );
  219. int send ( lo_address to, const char *path, double v );
  220. int send ( lo_address to, const char *path, int v );
  221. int send ( lo_address to, const char *path, long v );
  222. int send ( lo_address to, const char *path, int v1, int v2 );
  223. int send ( lo_address to, const char *path, int v1, float v2 );
  224. int send ( lo_address to, const char *path, const char *v );
  225. int send ( lo_address to, const char *path, const char *v1, float v2 );
  226. int send ( lo_address to, const char *path, const char *v1, int v2, int v3 );
  227. int send ( lo_address to, const char *path, const char *v1, const char *v2 );
  228. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3 );
  229. int send ( lo_address to, const char *path, const char *v1, int v2, int v3, int v4 );
  230. int send ( lo_address to, const char *path, const char *v1, const char *v2, int v3, int v4, int v5 );
  231. int send ( lo_address to, const char *path, const char *v1, int v2 );
  232. int send ( lo_address to, const char *path, int v1, const char *v2 );
  233. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, int v4, int v5, int v6 );
  234. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3 );
  235. int send ( lo_address to, const char *path, int v1, const char *v2, const char *v3, const char *v4 );
  236. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3, const char *v4, const char *v5 );
  237. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4, const char *v5 );
  238. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4 );
  239. int send ( lo_address to, const char *path, lo_message msg );
  240. int send ( lo_address to, const char *path, const char *v1, const char *v2, int v3, float v4, float v5, float v6 );
  241. // can be used to point back to owning object.
  242. void *owner;
  243. };
  244. };
  245. /* helper macros for defining OSC handlers */
  246. #define OSC_NAME( name ) osc_ ## name
  247. #define OSC_DMSG() DMESSAGE( "Got OSC message: %s", path );
  248. #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 )
  249. #define OSC_REPLY_OK() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "ok" )
  250. #define OSC_REPLY( value ) ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, value )
  251. #define OSC_REPLY_ERR() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "err" )
  252. #define OSC_ENDPOINT() ((OSC::Endpoint*)user_data)