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.

240 lines
7.1KB

  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. int pid;
  61. char * pid_str;
  62. jack_log("JackLibClient::Open name = %s", name);
  63. strncpy(fServerName, server_name, sizeof(fServerName));
  64. // Open server/client channel
  65. char name_res[JACK_CLIENT_NAME_SIZE + 1];
  66. if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
  67. jack_error("Cannot connect to the server");
  68. goto error;
  69. }
  70. // Start receiving notifications
  71. if (fChannel->Start() < 0) {
  72. jack_error("Cannot start channel");
  73. goto error;
  74. }
  75. pid = 0;
  76. pid_str = getenv("JACK_CLIENT_PID_OVERRIDE");
  77. if (pid_str != NULL)
  78. {
  79. pid = atoi(pid_str);
  80. }
  81. if (pid == 0)
  82. {
  83. pid = JackTools::GetPID();
  84. }
  85. // Require new client
  86. fChannel->ClientOpen(name_res, pid, &shared_engine, &shared_client, &shared_graph, &result);
  87. if (result < 0) {
  88. jack_error("Cannot open %s client", name_res);
  89. goto error;
  90. }
  91. try {
  92. // Map shared memory segments
  93. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  94. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  95. fClientControl.SetShmIndex(shared_client, fServerName);
  96. jack_verbose = GetEngineControl()->fVerbose;
  97. } catch (int n) {
  98. jack_error("Map shared memory segments exception %d", n);
  99. goto error;
  100. }
  101. SetupDriverSync(false);
  102. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  103. if (!fSynchroTable[GetClientControl()->fRefNum].Connect(name_res, fServerName)) {
  104. jack_error("Cannot ConnectSemaphore %s client", name_res);
  105. goto error;
  106. }
  107. JackGlobals::fClientTable[GetClientControl()->fRefNum] = this;
  108. JackGlobals::fServerRunning = true;
  109. SetClockSource(GetEngineControl()->fClockSource);
  110. jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
  111. return 0;
  112. error:
  113. fChannel->Stop();
  114. fChannel->Close();
  115. return -1;
  116. }
  117. // Notifications received from the server
  118. // TODO this should be done once for all clients in the process, when a shared notification channel
  119. // will be shared by all clients...
  120. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value1, int value2)
  121. {
  122. int res = 0;
  123. // Done all time
  124. switch (notify) {
  125. case kAddClient:
  126. jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
  127. // the synchro must be usable in I/O mode when several clients live in the same process
  128. res = fSynchroTable[refnum].Connect(name, fServerName) ? 0 : -1;
  129. break;
  130. case kRemoveClient:
  131. jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
  132. if (strcmp(GetClientControl()->fName, name) != 0)
  133. res = fSynchroTable[refnum].Disconnect() ? 0 : -1;
  134. break;
  135. }
  136. return res;
  137. }
  138. JackGraphManager* JackLibClient::GetGraphManager() const
  139. {
  140. assert(JackLibGlobals::fGlobals->fGraphManager);
  141. return JackLibGlobals::fGlobals->fGraphManager;
  142. }
  143. JackEngineControl* JackLibClient::GetEngineControl() const
  144. {
  145. assert(JackLibGlobals::fGlobals->fEngineControl);
  146. return JackLibGlobals::fGlobals->fEngineControl;
  147. }
  148. JackClientControl* JackLibClient::GetClientControl() const
  149. {
  150. return fClientControl;
  151. }
  152. int JackLibClient::PortConnect(const char* src, const char* dst)
  153. {
  154. // this check is to prevent apps to self connect to other apps
  155. // TODO: make this work with multiple clients per app
  156. {
  157. const char * sep_ptr;
  158. const char * client_name_ptr;
  159. int src_self;
  160. int dst_self;
  161. client_name_ptr = GetClientControl()->fName;
  162. //jack_info("Client '%s' connecting '%s' to '%s'", client_name_ptr, src, dst);
  163. sep_ptr = strchr(src, ':');
  164. if (sep_ptr == NULL)
  165. {
  166. jack_error("source port '%s' is invalid", src);
  167. return -1;
  168. }
  169. src_self = strncmp(client_name_ptr, src, sep_ptr - src) == 0 ? 0 : 1;
  170. sep_ptr = strchr(dst, ':');
  171. if (sep_ptr == NULL)
  172. {
  173. jack_error("destination port '%s' is invalid", dst);
  174. return -1;
  175. }
  176. dst_self = strncmp(client_name_ptr, dst, sep_ptr - dst) == 0 ? 0 : 1;
  177. //jack_info("src_self is %s", src_self ? "true" : "false");
  178. //jack_info("dst_self is %s", dst_self ? "true" : "false");
  179. // 0 means client is connecting other client ports (i.e. control app patchbay functionality)
  180. // 1 means client is connecting its own port to port of other client (i.e. self hooking into system app)
  181. // 2 means client is connecting its own ports (i.e. for app internal functionality)
  182. // TODO: Make this check an engine option and more tweakable (return error or success)
  183. // MAYBE: make the engine option changable on the fly and expose it through client or control API
  184. if (src_self + dst_self == 1)
  185. {
  186. jack_info("ignoring self hook to other client ports ('%s': '%s' -> '%s')", client_name_ptr, src, dst);
  187. return 0;
  188. }
  189. }
  190. return JackClient::PortConnect(src, dst);
  191. }
  192. } // end of namespace