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.

288 lines
8.0KB

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