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.

284 lines
7.7KB

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