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.

205 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. jack_client_t* jack_client_open_aux(const char* client_name, jack_options_t options, jack_status_t* status, va_list ap);
  31. SERVER_EXPORT jack_client_t * jack_client_open (const char *client_name,
  32. jack_options_t options,
  33. jack_status_t *status, ...);
  34. SERVER_EXPORT int jack_client_close (jack_client_t *client);
  35. SERVER_EXPORT int jack_get_client_pid (const char *name);
  36. #ifdef __cplusplus
  37. }
  38. #endif
  39. using namespace Jack;
  40. jack_client_t* jack_client_new_aux(const char* client_name, jack_options_t options, jack_status_t* status)
  41. {
  42. jack_varargs_t va; /* variable arguments */
  43. jack_status_t my_status;
  44. JackClient* client;
  45. if (client_name == NULL) {
  46. jack_error("jack_client_new called with a NULL client_name");
  47. return NULL;
  48. }
  49. jack_log("jack_client_new %s", client_name);
  50. if (status == NULL) /* no status from caller? */
  51. status = &my_status; /* use local status word */
  52. *status = (jack_status_t)0;
  53. /* validate parameters */
  54. if ((options & ~JackOpenOptions)) {
  55. int my_status1 = *status | (JackFailure | JackInvalidOption);
  56. *status = (jack_status_t)my_status1;
  57. return NULL;
  58. }
  59. /* parse variable arguments */
  60. jack_varargs_init(&va);
  61. if (!JackServerGlobals::Init()) { // jack server initialisation
  62. int my_status1 = (JackFailure | JackServerError);
  63. *status = (jack_status_t)my_status1;
  64. return NULL;
  65. }
  66. if (JACK_DEBUG) {
  67. client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
  68. } else {
  69. client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
  70. }
  71. int res = client->Open(va.server_name, client_name, va.session_id, options, status);
  72. if (res < 0) {
  73. delete client;
  74. JackServerGlobals::Destroy(); // jack server destruction
  75. int my_status1 = (JackFailure | JackServerError);
  76. *status = (jack_status_t)my_status1;
  77. return NULL;
  78. } else {
  79. return (jack_client_t*)client;
  80. }
  81. }
  82. jack_client_t* jack_client_open_aux(const char* client_name, jack_options_t options, jack_status_t* status, va_list ap)
  83. {
  84. jack_varargs_t va; /* variable arguments */
  85. jack_status_t my_status;
  86. JackClient* client;
  87. if (client_name == NULL) {
  88. jack_error("jack_client_open called with a NULL client_name");
  89. return NULL;
  90. }
  91. jack_log("jack_client_open %s", client_name);
  92. if (status == NULL) /* no status from caller? */
  93. status = &my_status; /* use local status word */
  94. *status = (jack_status_t)0;
  95. /* validate parameters */
  96. if ((options & ~JackOpenOptions)) {
  97. int my_status1 = *status | (JackFailure | JackInvalidOption);
  98. *status = (jack_status_t)my_status1;
  99. return NULL;
  100. }
  101. /* parse variable arguments */
  102. jack_varargs_parse(options, ap, &va);
  103. if (!JackServerGlobals::Init()) { // jack server initialisation
  104. int my_status1 = (JackFailure | JackServerError);
  105. *status = (jack_status_t)my_status1;
  106. return NULL;
  107. }
  108. if (JACK_DEBUG) {
  109. client = new JackDebugClient(new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable())); // Debug mode
  110. } else {
  111. client = new JackInternalClient(JackServerGlobals::fInstance, GetSynchroTable());
  112. }
  113. int res = client->Open(va.server_name, client_name, va.session_id, options, status);
  114. if (res < 0) {
  115. delete client;
  116. JackServerGlobals::Destroy(); // jack server destruction
  117. int my_status1 = (JackFailure | JackServerError);
  118. *status = (jack_status_t)my_status1;
  119. return NULL;
  120. } else {
  121. return (jack_client_t*)client;
  122. }
  123. }
  124. SERVER_EXPORT jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  125. {
  126. #ifdef __CLIENTDEBUG__
  127. JackGlobals::CheckContext("jack_client_open");
  128. #endif
  129. try {
  130. assert(JackGlobals::fOpenMutex);
  131. JackGlobals::fOpenMutex->Lock();
  132. va_list ap;
  133. va_start(ap, status);
  134. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  135. va_end(ap);
  136. JackGlobals::fOpenMutex->Unlock();
  137. return res;
  138. } catch(std::bad_alloc& e) {
  139. jack_error("Memory allocation error...");
  140. return NULL;
  141. } catch (...) {
  142. jack_error("Unknown error...");
  143. return NULL;
  144. }
  145. }
  146. SERVER_EXPORT int jack_client_close(jack_client_t* ext_client)
  147. {
  148. #ifdef __CLIENTDEBUG__
  149. JackGlobals::CheckContext("jack_client_close");
  150. #endif
  151. assert(JackGlobals::fOpenMutex);
  152. JackGlobals::fOpenMutex->Lock();
  153. int res = -1;
  154. jack_log("jack_client_close");
  155. JackClient* client = (JackClient*)ext_client;
  156. if (client == NULL) {
  157. jack_error("jack_client_close called with a NULL client");
  158. } else {
  159. res = client->Close();
  160. delete client;
  161. JackServerGlobals::Destroy(); // jack server destruction
  162. jack_log("jack_client_close res = %d", res);
  163. }
  164. JackGlobals::fOpenMutex->Unlock();
  165. return res;
  166. }
  167. SERVER_EXPORT int jack_get_client_pid(const char *name)
  168. {
  169. return (JackServerGlobals::fInstance != NULL)
  170. ? JackServerGlobals::fInstance->GetEngine()->GetClientPID(name)
  171. : 0;
  172. }