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.

156 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. // beware!!! things can go nasty if one client is started with JackNoStartServer and another without it
  42. static bool g_nostart;
  43. EXPORT jack_client_t* jack_client_open_aux(const char* ext_client_name, jack_options_t options, jack_status_t* status, va_list ap)
  44. {
  45. jack_varargs_t va; /* variable arguments */
  46. jack_status_t my_status;
  47. JackClient* client;
  48. char client_name[JACK_CLIENT_NAME_SIZE + 1];
  49. if (ext_client_name == NULL) {
  50. jack_error("jack_client_open called with a NULL client_name");
  51. return NULL;
  52. }
  53. jack_log("jack_client_open %s", ext_client_name);
  54. JackTools::RewriteName(ext_client_name, client_name);
  55. if (status == NULL) /* no status from caller? */
  56. status = &my_status; /* use local status word */
  57. *status = (jack_status_t)0;
  58. /* validate parameters */
  59. if ((options & ~JackOpenOptions)) {
  60. int my_status1 = *status | (JackFailure | JackInvalidOption);
  61. *status = (jack_status_t)my_status1;
  62. return NULL;
  63. }
  64. /* parse variable arguments */
  65. if (ap) {
  66. jack_varargs_parse(options, ap, &va);
  67. } else {
  68. jack_varargs_init(&va);
  69. }
  70. g_nostart = (options & JackNoStartServer) != 0;
  71. if (!g_nostart) {
  72. if (!JackServerGlobals::Init()) { // jack server initialisation
  73. int my_status1 = (JackFailure | JackServerError);
  74. *status = (jack_status_t)my_status1;
  75. return NULL;
  76. }
  77. }
  78. if (JACK_DEBUG) {
  79. client = new JackDebugClient(new JackInternalClient(JackServer::fInstance, GetSynchroTable())); // Debug mode
  80. } else {
  81. client = new JackInternalClient(JackServer::fInstance, GetSynchroTable());
  82. }
  83. int res = client->Open(va.server_name, client_name, options, status);
  84. if (res < 0) {
  85. delete client;
  86. if (!g_nostart) {
  87. JackServerGlobals::Destroy(); // jack server destruction
  88. }
  89. int my_status1 = (JackFailure | JackServerError);
  90. *status = (jack_status_t)my_status1;
  91. return NULL;
  92. } else {
  93. return (jack_client_t*)client;
  94. }
  95. }
  96. EXPORT jack_client_t* jack_client_open(const char* ext_client_name, jack_options_t options, jack_status_t* status, ...)
  97. {
  98. assert(JackGlobals::fOpenMutex);
  99. JackGlobals::fOpenMutex->Lock();
  100. va_list ap;
  101. va_start(ap, status);
  102. jack_client_t* res = jack_client_open_aux(ext_client_name, options, status, ap);
  103. va_end(ap);
  104. JackGlobals::fOpenMutex->Unlock();
  105. return res;
  106. }
  107. EXPORT int jack_client_close(jack_client_t* ext_client)
  108. {
  109. assert(JackGlobals::fOpenMutex);
  110. JackGlobals::fOpenMutex->Lock();
  111. int res = -1;
  112. jack_log("jack_client_close");
  113. JackClient* client = (JackClient*)ext_client;
  114. if (client == NULL) {
  115. jack_error("jack_client_close called with a NULL client");
  116. } else {
  117. res = client->Close();
  118. delete client;
  119. if (!g_nostart) {
  120. JackServerGlobals::Destroy(); // jack server destruction
  121. }
  122. jack_log("jack_client_close res = %d", res);
  123. }
  124. JackGlobals::fOpenMutex->Unlock();
  125. return res;
  126. }
  127. EXPORT int jack_get_client_pid(const char *name)
  128. {
  129. return (JackServer::fInstance != NULL)
  130. ? JackServer::fInstance->GetEngine()->GetClientPID(name)
  131. : 0;
  132. }