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.

256 lines
7.3KB

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