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.

279 lines
7.5KB

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