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.

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