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.

281 lines
7.8KB

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