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.

235 lines
7.9KB

  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. namespace OSC
  24. {
  25. typedef void * method_handle;
  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. class Endpoint
  85. {
  86. static void error_handler(int num, const char *msg, const char *path);
  87. static int osc_generic ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  88. static int osc_query_parameters ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data );
  89. Thread _thread;
  90. // lo_server_thread _st;
  91. lo_server _server;
  92. struct Parameter_Limits
  93. {
  94. float min;
  95. float max;
  96. float default_value;
  97. };
  98. struct Method_Data
  99. {
  100. char *path;
  101. char *typespec;
  102. char *documentation;
  103. struct Parameter_Limits *parameter_limits;
  104. std::list<lo_address> subscribers;
  105. Method_Data ( )
  106. {
  107. path = typespec = documentation = 0;
  108. parameter_limits = 0;
  109. }
  110. ~Method_Data ( )
  111. {
  112. if ( path )
  113. free( path );
  114. if ( typespec )
  115. free( typespec );
  116. if ( parameter_limits )
  117. free( parameter_limits );
  118. for ( std::list<lo_address>::iterator i = subscribers.begin();
  119. i != subscribers.end();
  120. ++i )
  121. {
  122. lo_address_free( *i );
  123. }
  124. subscribers.clear();
  125. }
  126. };
  127. std::list<Method_Data*> _methods;
  128. static void *osc_thread ( void *arg );
  129. void osc_thread ( void );
  130. public:
  131. int init ( const char *port = 0 );
  132. Endpoint ( );
  133. ~Endpoint ( );
  134. char *get_paths ( const char *prefix );
  135. method_handle add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data, const char *argument_description );
  136. void del_method ( const char *path, const char *typespec );
  137. void del_method ( const method_handle );
  138. void start ( void );
  139. void stop ( void );
  140. int port ( void ) const;
  141. char * url ( void ) const;
  142. void set_parameter_limits ( const char *path, const char *typespec, int index, float min, float max, float default_value );
  143. void check ( void ) const;
  144. void wait ( int timeout ) const;
  145. void run ( void ) const;
  146. int send ( lo_address to, const char *path, std::list< OSC_Value > values );
  147. /* overloads for common message formats */
  148. int send ( lo_address to, const char *path );
  149. int send ( lo_address to, const char *path, float v );
  150. int send ( lo_address to, const char *path, double v );
  151. int send ( lo_address to, const char *path, int v );
  152. int send ( lo_address to, const char *path, long v );
  153. int send ( lo_address to, const char *path, const char * v1, float v2 );
  154. int send ( lo_address to, const char *path, const char *v );
  155. int send ( lo_address to, const char *path, const char *v1, const char *v2 );
  156. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3 );
  157. int send ( lo_address to, const char *path, const char *v1, int v2, int v3, int v4 );
  158. int send ( lo_address to, const char *path, const char *v1, const char *v2, int v3, int v4, int v5 );
  159. int send ( lo_address to, const char *path, const char *v1, int v2 );
  160. int send ( lo_address to, const char *path, int v1, const char *v2 );
  161. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, int v4, int v5, int v6 );
  162. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3 );
  163. int send ( lo_address to, const char *path, int v1, const char *v2, const char *v3, const char *v4 );
  164. int send ( lo_address to, const char *path, const char *v1, int v2, const char *v3, const char *v4, const char *v5 );
  165. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4, const char *v5 );
  166. int send ( lo_address to, const char *path, const char *v1, const char *v2, const char *v3, const char *v4 );
  167. int send ( lo_address to, const char *path, lo_message msg );
  168. // can be used to point back to owning object.
  169. void *owner;
  170. };
  171. };
  172. /* helper macros for defining OSC handlers */
  173. #define OSC_NAME( name ) osc_ ## name
  174. #define OSC_DMSG() DMESSAGE( "Got OSC message: %s", path );
  175. #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 )
  176. #define OSC_REPLY_OK() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "ok" )
  177. #define OSC_REPLY( value ) ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, value )
  178. #define OSC_REPLY_ERR() ((OSC::Endpoint*)user_data)->send( lo_message_get_source( msg ), path, "err" )
  179. #define OSC_ENDPOINT() ((OSC::Endpoint*)user_data)