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.

202 lines
6.2KB

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