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.

252 lines
7.1KB

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