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.

228 lines
6.1KB

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