jack2 codebase
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.

208 lines
6.4KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "JackSystemDeps.h"
  17. #include "JackGraphManager.h"
  18. #include "JackInternalClient.h"
  19. #include "JackServer.h"
  20. #include "JackDebugClient.h"
  21. #include "JackServerGlobals.h"
  22. #include "JackTools.h"
  23. #include "JackCompilerDeps.h"
  24. #include "JackLockedEngine.h"
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. jack_client_t* jack_client_new_aux(const char* client_name, jack_options_t options, jack_status_t* status);
  30. SERVER_EXPORT jack_client_t * jack_client_open (const char *client_name,
  31. jack_options_t options,
  32. jack_status_t *status, ...);
  33. SERVER_EXPORT int jack_client_close (jack_client_t *client);
  34. SERVER_EXPORT int jack_get_client_pid (const char *name);
  35. #ifdef __cplusplus
  36. }
  37. #endif
  38. using namespace Jack;
  39. static jack_client_t * jack_client_open_aux (const char *client_name,
  40. jack_options_t options,
  41. jack_status_t *status, va_list ap);
  42. jack_client_t* jack_client_new_aux(const char* client_name, jack_options_t options, jack_status_t* status)
  43. {
  44. jack_varargs_t va; /* variable arguments */
  45. jack_status_t my_status;
  46. JackClient* client;
  47. if (client_name == NULL) {
  48. jack_error("jack_client_new called with a NULL client_name");
  49. return NULL;
  50. }
  51. jack_log("jack_client_new %s", client_name);
  52. if (status == NULL) /* no status from caller? */
  53. status = &my_status; /* use local status word */
  54. *status = (jack_status_t)0;
  55. /* validate parameters */
  56. if ((options & ~JackOpenOptions)) {
  57. int my_status1 = *status | (JackFailure | JackInvalidOption);
  58. *status = (jack_status_t)my_status1;
  59. return NULL;
  60. }
  61. /* parse variable arguments */
  62. jack_varargs_init(&va);
  63. if (!JackServerGlobals::Init()) { // jack server initialisation
  64. int my_status1 = (JackFailure | JackServerError);
  65. *status = (jack_status_t)my_status1;
  66. return NULL;
  67. }
  68. if (JACK_DEBUG) {
  69. client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
  70. } else {
  71. client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
  72. }
  73. int res = client->Open(va.server_name, client_name, va.session_id, options, status);
  74. if (res < 0) {
  75. delete client;
  76. JackServerGlobals::Destroy(); // jack server destruction
  77. int my_status1 = (JackFailure | JackServerError);
  78. *status = (jack_status_t)my_status1;
  79. return NULL;
  80. } else {
  81. return (jack_client_t*)client;
  82. }
  83. }
  84. jack_client_t* jack_client_open_aux(const char* client_name, jack_options_t options, jack_status_t* status, va_list ap)
  85. {
  86. jack_varargs_t va; /* variable arguments */
  87. jack_status_t my_status;
  88. JackClient* client;
  89. if (client_name == NULL) {
  90. jack_error("jack_client_open called with a NULL client_name");
  91. return NULL;
  92. }
  93. jack_log("jack_client_open %s", client_name);
  94. if (status == NULL) /* no status from caller? */
  95. status = &my_status; /* use local status word */
  96. *status = (jack_status_t)0;
  97. /* validate parameters */
  98. if ((options & ~JackOpenOptions)) {
  99. int my_status1 = *status | (JackFailure | JackInvalidOption);
  100. *status = (jack_status_t)my_status1;
  101. return NULL;
  102. }
  103. /* parse variable arguments */
  104. jack_varargs_parse(options, ap, &va);
  105. if (!JackServerGlobals::Init()) { // jack server initialisation
  106. int my_status1 = (JackFailure | JackServerError);
  107. *status = (jack_status_t)my_status1;
  108. return NULL;
  109. }
  110. if (JACK_DEBUG) {
  111. client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
  112. } else {
  113. client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
  114. }
  115. int res = client->Open(va.server_name, client_name, va.session_id, options, status);
  116. if (res < 0) {
  117. delete client;
  118. JackServerGlobals::Destroy(); // jack server destruction
  119. int my_status1 = (JackFailure | JackServerError);
  120. *status = (jack_status_t)my_status1;
  121. return NULL;
  122. } else {
  123. return (jack_client_t*)client;
  124. }
  125. }
  126. SERVER_EXPORT jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  127. {
  128. #ifdef __CLIENTDEBUG__
  129. JackGlobals::CheckContext("jack_client_open");
  130. #endif
  131. try {
  132. assert(JackGlobals::fOpenMutex);
  133. JackGlobals::fOpenMutex->Lock();
  134. va_list ap;
  135. va_start(ap, status);
  136. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  137. va_end(ap);
  138. JackGlobals::fOpenMutex->Unlock();
  139. return res;
  140. } catch(std::bad_alloc& e) {
  141. jack_error("Memory allocation error...");
  142. return NULL;
  143. } catch (...) {
  144. jack_error("Unknown error...");
  145. return NULL;
  146. }
  147. }
  148. SERVER_EXPORT int jack_client_close(jack_client_t* ext_client)
  149. {
  150. #ifdef __CLIENTDEBUG__
  151. JackGlobals::CheckContext("jack_client_close");
  152. #endif
  153. assert(JackGlobals::fOpenMutex);
  154. JackGlobals::fOpenMutex->Lock();
  155. int res = -1;
  156. jack_log("jack_client_close");
  157. JackClient* client = (JackClient*)ext_client;
  158. if (client == NULL) {
  159. jack_error("jack_client_close called with a NULL client");
  160. } else {
  161. res = client->Close();
  162. delete client;
  163. JackServerGlobals::Destroy(); // jack server destruction
  164. jack_log("jack_client_close res = %d", res);
  165. }
  166. JackGlobals::fOpenMutex->Unlock();
  167. return res;
  168. }
  169. SERVER_EXPORT int jack_get_client_pid(const char *name)
  170. {
  171. return (JackServerGlobals::fInstance != NULL)
  172. ? JackServerGlobals::fInstance->GetEngine()->GetClientPID(name)
  173. : 0;
  174. }