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.

220 lines
6.8KB

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