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.

219 lines
5.6KB

  1. /*******************************************************************************/
  2. /* Copyright (C) 2008 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 "Client.H"
  19. #include "Port.H"
  20. #include <algorithm>
  21. namespace JACK
  22. {
  23. nframes_t Client::_sample_rate = 0;
  24. Client::Client ( )
  25. {
  26. _freewheeling = false;
  27. _zombified = false;
  28. _client = NULL;
  29. _xruns = 0;
  30. }
  31. Client::~Client ( )
  32. {
  33. jack_client_close( _client );
  34. }
  35. /** Tell JACK to calling process callback. This MUST be called in
  36. * an inheriting class' destructor */
  37. void
  38. Client::deactivate ( )
  39. {
  40. jack_deactivate( _client );
  41. }
  42. /*******************/
  43. /* Static Wrappers */
  44. /*******************/
  45. int
  46. Client::process ( nframes_t nframes, void *arg )
  47. {
  48. return ((Client*)arg)->process( nframes );
  49. }
  50. int
  51. Client::sync ( jack_transport_state_t state, jack_position_t *pos, void *arg )
  52. {
  53. return ((Client*)arg)->sync( state, pos );
  54. }
  55. int
  56. Client::xrun ( void *arg )
  57. {
  58. ++((Client*)arg)->_xruns;
  59. return ((Client*)arg)->xrun();
  60. }
  61. void
  62. Client::timebase ( jack_transport_state_t state, jack_nframes_t nframes, jack_position_t *pos, int new_pos, void *arg )
  63. {
  64. ((Client*)arg)->timebase( state, nframes, pos, new_pos );
  65. }
  66. void
  67. Client::freewheel ( int starting, void *arg )
  68. {
  69. ((Client*)arg)->_freewheeling = starting;
  70. ((Client*)arg)->freewheel( starting );
  71. }
  72. int
  73. Client::buffer_size ( nframes_t nframes, void *arg )
  74. {
  75. return ((Client*)arg)->buffer_size( nframes );
  76. }
  77. void
  78. Client::thread_init ( void *arg )
  79. {
  80. ((Client*)arg)->thread_init();
  81. }
  82. void
  83. Client::shutdown ( void *arg )
  84. {
  85. ((Client*)arg)->_zombified = true;
  86. ((Client*)arg)->shutdown();
  87. }
  88. /** Connect to JACK using client name /client_name/. Return a static
  89. * pointer to actual name as reported by JACK */
  90. const char *
  91. Client::init ( const char *client_name )
  92. {
  93. if (( _client = jack_client_open ( client_name, (jack_options_t)0, NULL )) == 0 )
  94. return NULL;
  95. #define set_callback( name ) jack_set_ ## name ## _callback( _client, &Client:: name , this )
  96. set_callback( thread_init );
  97. set_callback( process );
  98. set_callback( xrun );
  99. set_callback( freewheel );
  100. set_callback( buffer_size );
  101. /* FIXME: should we wait to register this until after the project
  102. has been loaded (and we have disk threads running)? */
  103. set_callback( sync );
  104. jack_set_timebase_callback( _client, 0, &Client::timebase, this );
  105. jack_on_shutdown( _client, &Client::shutdown, this );
  106. jack_activate( _client );
  107. _sample_rate = frame_rate();
  108. return jack_get_client_name( _client );
  109. }
  110. /* THREAD: RT */
  111. /** enter or leave freehweeling mode */
  112. void
  113. Client::freewheeling ( bool yes )
  114. {
  115. if ( jack_set_freewheel( _client, yes ) )
  116. ;
  117. // WARNING( "Unkown error while setting freewheeling mode" );
  118. }
  119. void
  120. Client::port_added ( Port *p )
  121. {
  122. std::list < JACK::Port * >::iterator i = std::find( _active_ports.begin(), _active_ports.end(), p );
  123. if ( i != _active_ports.end() )
  124. return;
  125. _active_ports.push_back( p );
  126. }
  127. void
  128. Client::port_removed ( Port *p )
  129. {
  130. _active_ports.remove( p );
  131. }
  132. void
  133. Client::freeze_ports ( void )
  134. {
  135. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  136. i != _active_ports.end();
  137. ++i )
  138. {
  139. (*i)->freeze();
  140. }
  141. }
  142. void
  143. Client::thaw_ports ( void )
  144. {
  145. for ( std::list < JACK::Port * >::iterator i = _active_ports.begin();
  146. i != _active_ports.end();
  147. ++i )
  148. {
  149. (*i)->thaw();
  150. }
  151. }
  152. const char *
  153. Client::name ( const char *s )
  154. {
  155. /* Because the JACK API does not provide a mechanism for renaming
  156. * clients, we have to save connections, destroy our client,
  157. * create a client with the new name, and restore our
  158. * connections. Lovely. */
  159. freeze_ports();
  160. jack_deactivate( _client );
  161. jack_client_close( _client );
  162. _client = NULL;
  163. s = init( s );
  164. thaw_ports();
  165. return s;
  166. }
  167. }