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.

261 lines
7.4KB

  1. /*
  2. Copyright (C) 2001 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 "JackServerGlobals.h"
  18. #include "JackGraphManager.h"
  19. #include "JackConstants.h"
  20. #include "JackInternalClient.h"
  21. #include "JackLockedEngine.h"
  22. #include "JackServer.h"
  23. #include "JackEngineControl.h"
  24. #include "JackClientControl.h"
  25. #include "JackInternalClientChannel.h"
  26. #include "JackTools.h"
  27. #include <assert.h>
  28. namespace Jack
  29. {
  30. JackGraphManager* JackInternalClient::fGraphManager = NULL;
  31. JackEngineControl* JackInternalClient::fEngineControl = NULL;
  32. // Used for external C API (JackAPI.cpp)
  33. SERVER_EXPORT JackGraphManager* GetGraphManager()
  34. {
  35. return JackServerGlobals::fInstance->GetGraphManager();
  36. }
  37. SERVER_EXPORT JackEngineControl* GetEngineControl()
  38. {
  39. return JackServerGlobals::fInstance->GetEngineControl();
  40. }
  41. SERVER_EXPORT JackSynchro* GetSynchroTable()
  42. {
  43. return JackServerGlobals::fInstance->GetSynchroTable();
  44. }
  45. SERVER_EXPORT JackMetadata* GetMetadata()
  46. {
  47. return JackServerGlobals::fInstance->GetMetadata();
  48. }
  49. JackInternalClient::JackInternalClient(JackServer* server, JackSynchro* table): JackClient(table)
  50. {
  51. fChannel = new JackInternalClientChannel(server);
  52. }
  53. JackInternalClient::~JackInternalClient()
  54. {
  55. delete fChannel;
  56. }
  57. int JackInternalClient::Open(const char* server_name, const char* name, jack_uuid_t uuid, jack_options_t options, jack_status_t* status)
  58. {
  59. int result;
  60. jack_log("JackInternalClient::Open name = %s", name);
  61. if (strlen(name) >= JACK_CLIENT_NAME_SIZE) {
  62. jack_error("\"%s\" is too long to be used as a JACK client name.\n"
  63. "Please use %lu characters or less",
  64. name,
  65. JACK_CLIENT_NAME_SIZE - 1);
  66. return -1;
  67. }
  68. strncpy(fServerName, server_name, sizeof(fServerName));
  69. fServerName[sizeof(fServerName) - 1] = 0;
  70. // Open server/client direct channel
  71. char name_res[JACK_CLIENT_NAME_SIZE + 1];
  72. fChannel->ClientCheck(name, uuid, name_res, JACK_PROTOCOL_VERSION, (int)options, (int*)status, &result, false);
  73. if (result < 0) {
  74. int status1 = *status;
  75. if (status1 & JackVersionError) {
  76. jack_error("JACK protocol mismatch %d", JACK_PROTOCOL_VERSION);
  77. } else {
  78. jack_error("Client name = %s conflits with another running client", name);
  79. }
  80. goto error;
  81. }
  82. strcpy(fClientControl.fName, name_res);
  83. // Require new client
  84. fChannel->ClientOpen(name_res, &fClientControl.fRefNum, &fEngineControl, &fGraphManager, this, &result);
  85. if (result < 0) {
  86. jack_error("Cannot open client name = %s", name_res);
  87. goto error;
  88. }
  89. SetupDriverSync(false);
  90. JackGlobals::fClientTable[fClientControl.fRefNum] = this;
  91. JackGlobals::fServerRunning = true;
  92. jack_log("JackInternalClient::Open name = %s refnum = %ld", name_res, fClientControl.fRefNum);
  93. return 0;
  94. error:
  95. fChannel->Close();
  96. return -1;
  97. }
  98. void JackInternalClient::ShutDown(jack_status_t code, const char* message)
  99. {
  100. jack_log("JackInternalClient::ShutDown");
  101. JackClient::ShutDown(code, message);
  102. }
  103. JackGraphManager* JackInternalClient::GetGraphManager() const
  104. {
  105. assert(fGraphManager);
  106. return fGraphManager;
  107. }
  108. JackEngineControl* JackInternalClient::GetEngineControl() const
  109. {
  110. assert(fEngineControl);
  111. return fEngineControl;
  112. }
  113. JackClientControl* JackInternalClient::GetClientControl() const
  114. {
  115. return const_cast<JackClientControl*>(&fClientControl);
  116. }
  117. int JackLoadableInternalClient::Init(const char* so_name)
  118. {
  119. char path_to_so[JACK_PATH_MAX + 1];
  120. BuildClientPath(path_to_so, sizeof(path_to_so), so_name);
  121. fHandle = LoadJackModule(path_to_so);
  122. jack_log("JackLoadableInternalClient::JackLoadableInternalClient path_to_so = %s", path_to_so);
  123. if (fHandle == NULL) {
  124. PrintLoadError(so_name);
  125. return -1;
  126. }
  127. fFinish = (FinishCallback)GetJackProc(fHandle, "jack_finish");
  128. if (fFinish == NULL) {
  129. UnloadJackModule(fHandle);
  130. jack_error("symbol jack_finish cannot be found in %s", so_name);
  131. return -1;
  132. }
  133. fDescriptor = (JackDriverDescFunction)GetJackProc(fHandle, "jack_get_descriptor");
  134. if (fDescriptor == NULL) {
  135. jack_log("No jack_get_descriptor entry-point for %s", so_name);
  136. }
  137. return 0;
  138. }
  139. int JackLoadableInternalClient1::Init(const char* so_name)
  140. {
  141. if (JackLoadableInternalClient::Init(so_name) < 0) {
  142. return -1;
  143. }
  144. fInitialize = (InitializeCallback)GetJackProc(fHandle, "jack_initialize");
  145. if (fInitialize == NULL) {
  146. UnloadJackModule(fHandle);
  147. jack_error("symbol jack_initialize cannot be found in %s", so_name);
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. int JackLoadableInternalClient2::Init(const char* so_name)
  153. {
  154. if (JackLoadableInternalClient::Init(so_name) < 0) {
  155. return -1;
  156. }
  157. fInitialize = (InternalInitializeCallback)GetJackProc(fHandle, "jack_internal_initialize");
  158. if (fInitialize == NULL) {
  159. UnloadJackModule(fHandle);
  160. jack_error("symbol jack_internal_initialize cannot be found in %s", so_name);
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. JackLoadableInternalClient1::JackLoadableInternalClient1(JackServer* server, JackSynchro* table, const char* object_data)
  166. : JackLoadableInternalClient(server, table)
  167. {
  168. if (object_data != NULL)
  169. strncpy(fObjectData, object_data, JACK_LOAD_INIT_LIMIT);
  170. else
  171. memset(fObjectData, 0, sizeof(fObjectData));
  172. }
  173. JackLoadableInternalClient2::JackLoadableInternalClient2(JackServer* server, JackSynchro* table, const JSList* parameters)
  174. : JackLoadableInternalClient(server, table)
  175. {
  176. fParameters = parameters;
  177. }
  178. JackLoadableInternalClient::~JackLoadableInternalClient()
  179. {
  180. if (fFinish != NULL) {
  181. fFinish(fProcessArg);
  182. }
  183. if (fHandle != NULL) {
  184. UnloadJackModule(fHandle);
  185. }
  186. }
  187. int JackLoadableInternalClient1::Open(const char* server_name, const char* name, jack_uuid_t uuid, jack_options_t options, jack_status_t* status)
  188. {
  189. int res = -1;
  190. if (JackInternalClient::Open(server_name, name, uuid, options, status) == 0) {
  191. if (fInitialize((jack_client_t*)this, fObjectData) == 0) {
  192. res = 0;
  193. } else {
  194. JackInternalClient::Close();
  195. fFinish = NULL;
  196. }
  197. }
  198. return res;
  199. }
  200. int JackLoadableInternalClient2::Open(const char* server_name, const char* name, jack_uuid_t uuid, jack_options_t options, jack_status_t* status)
  201. {
  202. int res = -1;
  203. if (JackInternalClient::Open(server_name, name, uuid, options, status) == 0) {
  204. if (fInitialize((jack_client_t*)this, fParameters) == 0) {
  205. res = 0;
  206. } else {
  207. JackInternalClient::Close();
  208. fFinish = NULL;
  209. }
  210. }
  211. return res;
  212. }
  213. } // end of namespace