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.

158 lines
4.7KB

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