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.

287 lines
7.9KB

  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. #ifdef WIN32
  31. static void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
  32. {
  33. snprintf(path_to_so, path_len, ADDON_DIR "/%s.dll", so_name);
  34. }
  35. static void PrintLoadError(const char* so_name)
  36. {
  37. // Retrieve the system error message for the last-error code
  38. LPVOID lpMsgBuf;
  39. LPVOID lpDisplayBuf;
  40. DWORD dw = GetLastError();
  41. FormatMessage(
  42. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  43. FORMAT_MESSAGE_FROM_SYSTEM |
  44. FORMAT_MESSAGE_IGNORE_INSERTS,
  45. NULL,
  46. dw,
  47. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  48. (LPTSTR) &lpMsgBuf,
  49. 0, NULL );
  50. // Display the error message and exit the process
  51. lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
  52. (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)so_name) + 40) * sizeof(TCHAR));
  53. _snprintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR),
  54. TEXT("error loading %s err = %s"), so_name, lpMsgBuf);
  55. jack_error((LPCTSTR)lpDisplayBuf);
  56. LocalFree(lpMsgBuf);
  57. LocalFree(lpDisplayBuf);
  58. }
  59. #else
  60. static void BuildClientPath(char* path_to_so, int path_len, const char* so_name)
  61. {
  62. const char* internal_dir;
  63. if ((internal_dir = getenv("JACK_INTERNAL_DIR")) == 0) {
  64. if ((internal_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  65. internal_dir = ADDON_DIR;
  66. }
  67. }
  68. snprintf(path_to_so, path_len, "%s/%s.so", internal_dir, 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 JackServerGlobals::fInstance->GetGraphManager();
  77. }
  78. JackEngineControl* GetEngineControl()
  79. {
  80. return JackServerGlobals::fInstance->GetEngineControl();
  81. }
  82. JackSynchro* GetSynchroTable()
  83. {
  84. return JackServerGlobals::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. strncpy(fServerName, server_name, sizeof(fServerName));
  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. JackGlobals::fClientTable[fClientControl.fRefNum] = this;
  118. JackGlobals::fServerRunning = true;
  119. jack_log("JackInternalClient::Open name = %s refnum = %ld", name_res, fClientControl.fRefNum);
  120. return 0;
  121. error:
  122. fChannel->Stop();
  123. fChannel->Close();
  124. return -1;
  125. }
  126. JackGraphManager* JackInternalClient::GetGraphManager() const
  127. {
  128. assert(fGraphManager);
  129. return fGraphManager;
  130. }
  131. JackEngineControl* JackInternalClient::GetEngineControl() const
  132. {
  133. assert(fEngineControl);
  134. return fEngineControl;
  135. }
  136. JackClientControl* JackInternalClient::GetClientControl() const
  137. {
  138. return const_cast<JackClientControl*>(&fClientControl);
  139. }
  140. int JackLoadableInternalClient::Init(const char* so_name)
  141. {
  142. char path_to_so[JACK_PATH_MAX + 1];
  143. BuildClientPath(path_to_so, sizeof(path_to_so), so_name);
  144. fHandle = LoadJackModule(path_to_so);
  145. jack_log("JackLoadableInternalClient::JackLoadableInternalClient path_to_so = %s", path_to_so);
  146. if (fHandle == NULL) {
  147. PrintLoadError(so_name);
  148. return -1;
  149. }
  150. fFinish = (FinishCallback)GetJackProc(fHandle, "jack_finish");
  151. if (fFinish == NULL) {
  152. UnloadJackModule(fHandle);
  153. jack_error("symbol jack_finish cannot be found in %s", so_name);
  154. return -1;
  155. }
  156. fDescriptor = (JackDriverDescFunction)GetJackProc(fHandle, "jack_get_descriptor");
  157. if (fDescriptor == NULL) {
  158. jack_info("No jack_get_descriptor entry-point for %s", so_name);
  159. }
  160. return 0;
  161. }
  162. int JackLoadableInternalClient1::Init(const char* so_name)
  163. {
  164. if (JackLoadableInternalClient::Init(so_name) < 0) {
  165. return -1;
  166. }
  167. fInitialize = (InitializeCallback)GetJackProc(fHandle, "jack_initialize");
  168. if (fInitialize == NULL) {
  169. UnloadJackModule(fHandle);
  170. jack_error("symbol jack_initialize cannot be found in %s", so_name);
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. int JackLoadableInternalClient2::Init(const char* so_name)
  176. {
  177. if (JackLoadableInternalClient::Init(so_name) < 0) {
  178. return -1;
  179. }
  180. fInitialize = (InternalInitializeCallback)GetJackProc(fHandle, "jack_internal_initialize");
  181. if (fInitialize == NULL) {
  182. UnloadJackModule(fHandle);
  183. jack_error("symbol jack_internal_initialize cannot be found in %s", so_name);
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. JackLoadableInternalClient1::JackLoadableInternalClient1(JackServer* server, JackSynchro* table, const char* object_data)
  189. : JackLoadableInternalClient(server, table)
  190. {
  191. strncpy(fObjectData, object_data, JACK_LOAD_INIT_LIMIT);
  192. }
  193. JackLoadableInternalClient2::JackLoadableInternalClient2(JackServer* server, JackSynchro* table, const JSList* parameters)
  194. : JackLoadableInternalClient(server, table)
  195. {
  196. fParameters = parameters;
  197. }
  198. JackLoadableInternalClient::~JackLoadableInternalClient()
  199. {
  200. if (fFinish != NULL)
  201. fFinish(fProcessArg);
  202. if (fHandle != NULL)
  203. UnloadJackModule(fHandle);
  204. }
  205. int JackLoadableInternalClient1::Open(const char* server_name, const char* name, jack_options_t options, jack_status_t* status)
  206. {
  207. int res = -1;
  208. if (JackInternalClient::Open(server_name, name, options, status) == 0) {
  209. if (fInitialize((jack_client_t*)this, fObjectData) == 0) {
  210. res = 0;
  211. } else {
  212. JackInternalClient::Close();
  213. fFinish = NULL;
  214. }
  215. }
  216. return res;
  217. }
  218. int JackLoadableInternalClient2::Open(const char* server_name, const char* name, jack_options_t options, jack_status_t* status)
  219. {
  220. int res = -1;
  221. if (JackInternalClient::Open(server_name, name, options, status) == 0) {
  222. if (fInitialize((jack_client_t*)this, fParameters) == 0) {
  223. res = 0;
  224. } else {
  225. JackInternalClient::Close();
  226. fFinish = NULL;
  227. }
  228. }
  229. return res;
  230. }
  231. } // end of namespace