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.

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