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.

263 lines
7.6KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "JackLibClient.h"
  16. #include "JackTime.h"
  17. #include "JackLibGlobals.h"
  18. #include "JackGlobals.h"
  19. #include "JackPlatformPlug.h"
  20. #include "JackTools.h"
  21. namespace Jack
  22. {
  23. // Used for external C API (JackAPI.cpp)
  24. JackGraphManager* GetGraphManager()
  25. {
  26. if (JackLibGlobals::fGlobals) {
  27. return JackLibGlobals::fGlobals->fGraphManager;
  28. } else {
  29. return NULL;
  30. }
  31. }
  32. JackEngineControl* GetEngineControl()
  33. {
  34. if (JackLibGlobals::fGlobals) {
  35. return JackLibGlobals::fGlobals->fEngineControl;
  36. } else {
  37. return NULL;
  38. }
  39. }
  40. JackSynchro* GetSynchroTable()
  41. {
  42. return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
  43. }
  44. //-------------------
  45. // Client management
  46. //-------------------
  47. JackLibClient::JackLibClient(JackSynchro* table): JackClient(table)
  48. {
  49. jack_log("JackLibClient::JackLibClient table = %x", table);
  50. fChannel = new JackClientChannel();
  51. }
  52. JackLibClient::~JackLibClient()
  53. {
  54. jack_log("JackLibClient::~JackLibClient");
  55. delete fChannel;
  56. }
  57. int JackLibClient::Open(const char* server_name, const char* name, jack_options_t options, jack_status_t* status)
  58. {
  59. int shared_engine, shared_client, shared_graph, result;
  60. jack_log("JackLibClient::Open name = %s", name);
  61. strncpy(fServerName, server_name, sizeof(fServerName));
  62. // Open server/client channel
  63. char name_res[JACK_CLIENT_NAME_SIZE + 1];
  64. if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
  65. jack_error("Cannot connect to the server");
  66. goto error;
  67. }
  68. // Start receiving notifications
  69. if (fChannel->Start() < 0) {
  70. jack_error("Cannot start channel");
  71. goto error;
  72. }
  73. // Require new client
  74. fChannel->ClientOpen(name_res, JackTools::GetPID(), &shared_engine, &shared_client, &shared_graph, &result);
  75. if (result < 0) {
  76. jack_error("Cannot open %s client", name_res);
  77. goto error;
  78. }
  79. try {
  80. // Map shared memory segments
  81. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  82. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  83. fClientControl.SetShmIndex(shared_client, fServerName);
  84. jack_verbose = GetEngineControl()->fVerbose;
  85. } catch (int n) {
  86. jack_error("Map shared memory segments exception %d", n);
  87. goto error;
  88. }
  89. SetupDriverSync(false);
  90. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  91. if (!fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName)) {
  92. jack_error("Cannot ConnectSemaphore %s client", name_res);
  93. goto error;
  94. }
  95. JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
  96. JackGlobals::fServerRunning = true;
  97. SetClockSource(GetEngineControl()->fClockSource);
  98. jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
  99. return 0;
  100. error:
  101. fChannel->Stop();
  102. fChannel->Close();
  103. return -1;
  104. }
  105. // Notifications received from the server
  106. // TODO this should be done once for all clients in the process, when a shared notification channel
  107. // will be shared by all clients...
  108. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value1, int value2)
  109. {
  110. int res = 0;
  111. // Done all time
  112. switch (notify) {
  113. case kAddClient:
  114. jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
  115. // the synchro must be usable in I/O mode when several clients live in the same process
  116. res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
  117. break;
  118. case kRemoveClient:
  119. jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
  120. if (strcmp(GetClientControl()->fName, name) != 0)
  121. res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
  122. break;
  123. }
  124. return res;
  125. }
  126. JackGraphManager* JackLibClient::GetGraphManager() const
  127. {
  128. assert(JackLibGlobals::fGlobals->fGraphManager);
  129. return JackLibGlobals::fGlobals->fGraphManager;
  130. }
  131. JackEngineControl* JackLibClient::GetEngineControl() const
  132. {
  133. assert(JackLibGlobals::fGlobals->fEngineControl);
  134. return JackLibGlobals::fGlobals->fEngineControl;
  135. }
  136. JackClientControl* JackLibClient::GetClientControl() const
  137. {
  138. return fClientControl;
  139. }
  140. int
  141. JackLibClient::NoSelfConnectCheck(const char* src, const char* dst)
  142. {
  143. // this check is to prevent apps to self connect to other apps
  144. // TODO: make this work with multiple clients per app
  145. {
  146. const char * sep_ptr;
  147. const char * client_name_ptr;
  148. int src_self;
  149. int dst_self;
  150. client_name_ptr = GetClientControl()->fName;
  151. //jack_info("Client '%s' (dis)connecting '%s' to '%s'", client_name_ptr, src, dst);
  152. sep_ptr = strchr(src, ':');
  153. if (sep_ptr == NULL)
  154. {
  155. jack_error("source port '%s' is invalid", src);
  156. return -1;
  157. }
  158. src_self = strncmp(client_name_ptr, src, sep_ptr - src) == 0 ? 0 : 1;
  159. sep_ptr = strchr(dst, ':');
  160. if (sep_ptr == NULL)
  161. {
  162. jack_error("destination port '%s' is invalid", dst);
  163. return -1;
  164. }
  165. dst_self = strncmp(client_name_ptr, dst, sep_ptr - dst) == 0 ? 0 : 1;
  166. //jack_info("src_self is %s", src_self ? "true" : "false");
  167. //jack_info("dst_self is %s", dst_self ? "true" : "false");
  168. // 0 means client is connecting other client ports (i.e. control app patchbay functionality)
  169. // 1 means client is connecting its own port to port of other client (i.e. self hooking into system app)
  170. // 2 means client is connecting its own ports (i.e. for app internal functionality)
  171. // TODO: Make this check an engine option and more tweakable (return error or success)
  172. // MAYBE: make the engine option changable on the fly and expose it through client or control API
  173. if (src_self + dst_self != 0)
  174. {
  175. jack_info("ignoring self hook to other client ports ('%s': '%s' -> '%s')", client_name_ptr, src, dst);
  176. return 0;
  177. }
  178. }
  179. return 1;
  180. }
  181. int JackLibClient::PortConnect(const char* src, const char* dst)
  182. {
  183. int ret;
  184. //jack_info("Client connecting '%s' to '%s'", src, dst);
  185. ret = NoSelfConnectCheck(src, dst);
  186. if (ret > 0)
  187. {
  188. return JackClient::PortConnect(src, dst);
  189. }
  190. return ret;
  191. }
  192. int JackLibClient::PortDisconnect(const char* src, const char* dst)
  193. {
  194. int ret;
  195. //jack_info("Client disconnecting '%s' to '%s'", src, dst);
  196. ret = NoSelfConnectCheck(src, dst);
  197. if (ret > 0)
  198. {
  199. return JackClient::PortDisconnect(src, dst);
  200. }
  201. return ret;
  202. }
  203. int JackLibClient::PortDisconnect(jack_port_id_t src)
  204. {
  205. jack_info("Ignoring port disconnect request");
  206. return 0;
  207. }
  208. } // end of namespace