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.

232 lines
6.2KB

  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 "Endpoint.H"
  23. namespace OSC
  24. {
  25. void
  26. Endpoint::error_handler(int num, const char *msg, const char *path)
  27. {
  28. WARNING( "LibLO server error %d in path %s: %s\n", num, path, msg);
  29. }
  30. Endpoint::Endpoint ( const char *port )
  31. {
  32. DMESSAGE( "Creating OSC server" );
  33. // _st = lo_server_thread_new( s, error_handler );
  34. // _server = lo_server_thread_get_server( _st );
  35. _server = lo_server_new( port, error_handler );
  36. if ( ! _server )
  37. FATAL( "Error creating OSC server" );
  38. char *url = lo_server_get_url(_server);
  39. printf("OSC: %s\n",url);
  40. free(url);
  41. }
  42. Endpoint::~Endpoint ( )
  43. {
  44. // lo_server_thread_free( _st );
  45. lo_server_free( _server );
  46. }
  47. void
  48. Endpoint::add_method ( const char *path, const char *typespec, lo_method_handler handler, void *user_data )
  49. {
  50. DMESSAGE( "Added OSC method %s (%s)", path, typespec );
  51. lo_server_add_method( _server, path, typespec, handler, user_data );
  52. }
  53. void
  54. Endpoint::del_method ( const char *path, const char *typespec )
  55. {
  56. DMESSAGE( "Deleted OSC method %s (%s)", path, typespec );
  57. lo_server_del_method( _server, path, typespec );
  58. }
  59. /* void * */
  60. /* Endpoint::osc_thread ( void * arg ) */
  61. /* { */
  62. /* ((Endpoint*)arg)->osc_thread(); */
  63. /* return NULL; */
  64. /* } */
  65. /* void */
  66. /* Endpoint::osc_thread ( void ) */
  67. /* { */
  68. /* _thread.name( "OSC" ); */
  69. /* DMESSAGE( "OSC Thread running" ); */
  70. /* for ( ;; ) */
  71. /* { */
  72. /* lo_server_recv( _sever ); */
  73. /* } */
  74. /* } */
  75. void
  76. Endpoint::start ( void )
  77. {
  78. /* if ( !_thread.clone( &Endpoint::osc_thread, this ) ) */
  79. /* FATAL( "Could not create OSC thread" ); */
  80. /* lo_server_thread_start( _st ); */
  81. }
  82. void
  83. Endpoint::stop ( void )
  84. {
  85. // lo_server_thread_stop( _st );
  86. }
  87. int
  88. Endpoint::port ( void ) const
  89. {
  90. return lo_server_get_port( _server );
  91. }
  92. char *
  93. Endpoint::url ( void ) const
  94. {
  95. return lo_server_get_url( _server );
  96. }
  97. /** Process any waiting events and return immediately */
  98. void
  99. Endpoint::check ( void ) const
  100. {
  101. wait( 0 );
  102. }
  103. /** Process any waiting events and return immediately */
  104. void
  105. Endpoint::wait ( int timeout ) const
  106. {
  107. lo_server_recv_noblock( _server, timeout );
  108. }
  109. /** Process events forever */
  110. void
  111. Endpoint::run ( void ) const
  112. {
  113. for ( ;; )
  114. {
  115. lo_server_recv( _server );
  116. }
  117. }
  118. int
  119. Endpoint::send ( lo_address to, const char *path, std::list< OSC_Value > values )
  120. {
  121. lo_message m = lo_message_new();
  122. for ( std::list< OSC_Value >::const_iterator i = values.begin();
  123. i != values.end();
  124. ++i )
  125. {
  126. const OSC_Value *ov = &(*i);
  127. switch ( ov->type() )
  128. {
  129. case 'f':
  130. lo_message_add_float( m, ((OSC_Float*)ov)->value() );
  131. break;
  132. case 'i':
  133. lo_message_add_int32( m, ((OSC_Int*)ov)->value() );
  134. break;
  135. case 's':
  136. DMESSAGE( "Adding string %s", ((OSC_String*)ov)->value() );
  137. lo_message_add_string( m, ((OSC_String*)ov)->value() );
  138. break;
  139. default:
  140. FATAL( "Unknown format: %c", ov->type() );
  141. break;
  142. }
  143. }
  144. DMESSAGE( "Path: %s", path );
  145. lo_bundle b = lo_bundle_new( LO_TT_IMMEDIATE );
  146. lo_bundle_add_message(b, path, m );
  147. int r = lo_send_bundle_from( to, _server, b );
  148. // int r = lo_send_message_from( to, _server, path, m );
  149. // lo_message_free( m );
  150. return r;
  151. }
  152. int
  153. Endpoint::send ( lo_address to, const char *path )
  154. {
  155. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "" );
  156. }
  157. int
  158. Endpoint::send ( lo_address to, const char *path, int v )
  159. {
  160. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "i", v );
  161. }
  162. int
  163. Endpoint::send ( lo_address to, const char *path, float v )
  164. {
  165. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "f", v );
  166. }
  167. int
  168. Endpoint::send ( lo_address to, const char *path, double v )
  169. {
  170. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "d", v );
  171. }
  172. int
  173. Endpoint::send ( lo_address to, const char *path, const char * v )
  174. {
  175. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "s", v );
  176. }
  177. int
  178. Endpoint::send ( lo_address to, const char *path, const char * v1, const char *v2 )
  179. {
  180. return lo_send_from( to, _server, LO_TT_IMMEDIATE, path, "ss", v1, v2 );
  181. }
  182. }