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