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.

300 lines
8.0KB

  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. #include <lo/lo.h>
  19. #include "debug.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "Endpoint.H"
  24. namespace OSC
  25. {
  26. void
  27. Endpoint::error_handler(int num, const char *msg, const char *path)
  28. {
  29. WARNING( "LibLO server error %d in path %s: %s\n", num, path, msg);
  30. }
  31. Endpoint::Endpoint ( const char *port )
  32. {
  33. DMESSAGE( "Creating OSC server" );
  34. // _st = lo_server_thread_new( s, error_handler );
  35. // _server = lo_server_thread_get_server( _st );
  36. _server = lo_server_new( port, error_handler );
  37. if ( ! _server )
  38. FATAL( "Error creating OSC server" );
  39. char *url = lo_server_get_url(_server);
  40. printf("OSC: %s\n",url);
  41. free(url);
  42. add_method( NULL, "", &Endpoint::osc_generic, this, "" );
  43. // _path_names = new std::list<const char*>();
  44. }
  45. Endpoint::~Endpoint ( )
  46. {
  47. // lo_server_thread_free( _st );
  48. lo_server_free( _server );
  49. }
  50. int
  51. Endpoint::osc_generic ( const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *user_data )
  52. {
  53. OSC_DMSG();
  54. if ( path[ strlen(path) - 1 ] != '/' )
  55. {
  56. DMESSAGE( "Unknown OSC signal %s", path );
  57. return 0;
  58. }
  59. char *paths = ((Endpoint*)user_data)->get_paths( path );
  60. ((Endpoint*)user_data)->send( lo_message_get_source( msg ), "/reply", path, paths );
  61. free(paths);
  62. return 0;
  63. }
  64. // returns a malloc()'d string containing path names beginning with /prefix/, newline separated
  65. char *
  66. Endpoint::get_paths ( const char *prefix )
  67. {
  68. char *r = (char*)malloc( 1024 );
  69. r[0] = 0;
  70. for ( std::list<char*>::iterator i = _path_names.begin(); i != _path_names.end(); ++i )
  71. {
  72. if ( ! *i )
  73. continue;
  74. if (! strncmp( *i, prefix, strlen(prefix) ) )
  75. {
  76. r = (char*)realloc( r, strlen( r ) + strlen( *i ) + 2 );
  77. strcat( r, *i );
  78. strcat( r, "\n" );
  79. }
  80. }
  81. return r;
  82. }
  83. void
  84. Endpoint::add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data, const char *argument_description )
  85. {
  86. DMESSAGE( "Added OSC method %s (%s)", path, typespec );
  87. lo_server_add_method( _server, path, typespec, handler, user_data );
  88. char *stored_path;
  89. asprintf( &stored_path, "%s (%s); %s", path, typespec, argument_description );
  90. _path_names.push_back( stored_path );
  91. }
  92. void
  93. Endpoint::del_method ( const char *path, const char *typespec )
  94. {
  95. DMESSAGE( "Deleted OSC method %s (%s)", path, typespec );
  96. lo_server_del_method( _server, path, typespec );
  97. for ( std::list<char *>::iterator i = _path_names.begin(); i != _path_names.end(); ++i )
  98. {
  99. if ( ! *i )
  100. continue;
  101. if ( ! strncmp( path, *i, index( *i, ' ' ) - *i ) )
  102. {
  103. free( *i );
  104. i = _path_names.erase( i );
  105. }
  106. }
  107. }
  108. /* void * */
  109. /* Endpoint::osc_thread ( void * arg ) */
  110. /* { */
  111. /* ((Endpoint*)arg)->osc_thread(); */
  112. /* return NULL; */
  113. /* } */
  114. /* void */
  115. /* Endpoint::osc_thread ( void ) */
  116. /* { */
  117. /* _thread.name( "OSC" ); */
  118. /* DMESSAGE( "OSC Thread running" ); */
  119. /* for ( ;; ) */
  120. /* { */
  121. /* lo_server_recv( _sever ); */
  122. /* } */
  123. /* } */
  124. void
  125. Endpoint::start ( void )
  126. {
  127. /* if ( !_thread.clone( &Endpoint::osc_thread, this ) ) */
  128. /* FATAL( "Could not create OSC thread" ); */
  129. /* lo_server_thread_start( _st ); */
  130. }
  131. void
  132. Endpoint::stop ( void )
  133. {
  134. // lo_server_thread_stop( _st );
  135. }
  136. int
  137. Endpoint::port ( void ) const
  138. {
  139. return lo_server_get_port( _server );
  140. }
  141. char *
  142. Endpoint::url ( void ) const
  143. {
  144. return lo_server_get_url( _server );
  145. }
  146. /** Process any waiting events and return immediately */
  147. void
  148. Endpoint::check ( void ) const
  149. {
  150. wait( 0 );
  151. }
  152. /** Process any waiting events and return immediately */
  153. void
  154. Endpoint::wait ( int timeout ) const
  155. {
  156. lo_server_recv_noblock( _server, timeout );
  157. }
  158. /** Process events forever */
  159. void
  160. Endpoint::run ( void ) const
  161. {
  162. for ( ;; )
  163. {
  164. lo_server_recv( _server );
  165. }
  166. }
  167. int
  168. Endpoint::send ( lo_address to, const char *path, std::list< OSC_Value > values )
  169. {
  170. lo_message m = lo_message_new();
  171. for ( std::list< OSC_Value >::const_iterator i = values.begin();
  172. i != values.end();
  173. ++i )
  174. {
  175. const OSC_Value *ov = &(*i);
  176. switch ( ov->type() )
  177. {
  178. case 'f':
  179. lo_message_add_float( m, ((OSC_Float*)ov)->value() );
  180. break;
  181. case 'i':
  182. lo_message_add_int32( m, ((OSC_Int*)ov)->value() );
  183. break;
  184. case 's':
  185. DMESSAGE( "Adding string %s", ((OSC_String*)ov)->value() );
  186. lo_message_add_string( m, ((OSC_String*)ov)->value() );
  187. break;
  188. default:
  189. FATAL( "Unknown format: %c", ov->type() );
  190. break;
  191. }
  192. }
  193. DMESSAGE( "Path: %s", path );
  194. lo_bundle b = lo_bundle_new( LO_TT_IMMEDIATE );
  195. lo_bundle_add_message(b, path, m );
  196. int r = lo_send_bundle_from( to, _server, b );
  197. // int r = lo_send_message_from( to, _server, path, m );
  198. // lo_message_free( m );
  199. return r;
  200. }
  201. int
  202. Endpoint::send ( lo_address to, const char *path )
  203. {
  204. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "" );
  205. }
  206. int
  207. Endpoint::send ( lo_address to, const char *path, int v )
  208. {
  209. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "i", v );
  210. }
  211. int
  212. Endpoint::send ( lo_address to, const char *path, float v )
  213. {
  214. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "f", v );
  215. }
  216. int
  217. Endpoint::send ( lo_address to, const char *path, double v )
  218. {
  219. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "d", v );
  220. }
  221. int
  222. Endpoint::send ( lo_address to, const char *path, const char * v )
  223. {
  224. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "s", v );
  225. }
  226. int
  227. Endpoint::send ( lo_address to, const char *path, const char * v1, const char *v2 )
  228. {
  229. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "ss", v1, v2 );
  230. }
  231. }