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.

181 lines
5.2KB

  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 "JackChannel.h"
  20. namespace Jack
  21. {
  22. // Used for external C API (JackAPI.cpp)
  23. JackGraphManager* GetGraphManager()
  24. {
  25. if (JackLibGlobals::fGlobals) {
  26. return JackLibGlobals::fGlobals->fGraphManager;
  27. } else {
  28. return NULL;
  29. }
  30. }
  31. JackEngineControl* GetEngineControl()
  32. {
  33. if (JackLibGlobals::fGlobals) {
  34. return JackLibGlobals::fGlobals->fEngineControl;
  35. } else {
  36. return NULL;
  37. }
  38. }
  39. JackSynchro** GetSynchroTable()
  40. {
  41. return (JackLibGlobals::fGlobals ? JackLibGlobals::fGlobals->fSynchroTable : 0);
  42. }
  43. //-------------------
  44. // Client management
  45. //-------------------
  46. JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
  47. {
  48. jack_log("JackLibClient::JackLibClient table = %x", table);
  49. fChannel = JackGlobals::MakeClientChannel();
  50. }
  51. JackLibClient::~JackLibClient()
  52. {
  53. jack_log("JackLibClient::~JackLibClient");
  54. delete fChannel;
  55. }
  56. int JackLibClient::Open(const char* server_name, const char* name, jack_options_t options, jack_status_t* status)
  57. {
  58. int shared_engine, shared_client, shared_graph, result;
  59. jack_log("JackLibClient::Open %s", name);
  60. snprintf(fServerName, sizeof(fServerName), server_name);
  61. // Open server/client channel
  62. char name_res[JACK_CLIENT_NAME_SIZE];
  63. if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
  64. jack_error("Cannot connect to the server");
  65. goto error;
  66. }
  67. // Start receiving notifications
  68. if (fChannel->Start() < 0) {
  69. jack_error("Cannot start channel");
  70. goto error;
  71. }
  72. // Require new client
  73. fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
  74. if (result < 0) {
  75. jack_error("Cannot open %s client", name_res);
  76. goto error;
  77. }
  78. try {
  79. // Map shared memory segments
  80. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  81. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  82. fClientControl.SetShmIndex(shared_client, fServerName);
  83. jack_verbose = GetEngineControl()->fVerbose;
  84. } catch (int n) {
  85. jack_error("Map shared memory segments exception %d", n);
  86. goto error;
  87. }
  88. SetupDriverSync(false);
  89. /* TODO : solve WIN32 thread Kill issue
  90. #ifndef WIN32
  91. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  92. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
  93. jack_error("Cannot ConnectSemaphore %s client", name);
  94. goto error;
  95. }
  96. #endif
  97. */
  98. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  99. if (!fSynchroTable[GetClientControl()->fRefNum]->Connect(name_res, fServerName)) {
  100. jack_error("Cannot ConnectSemaphore %s client", name_res);
  101. goto error;
  102. }
  103. jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
  104. return 0;
  105. error:
  106. fChannel->Stop();
  107. fChannel->Close();
  108. return -1;
  109. }
  110. // Notifications received from the server
  111. // TODO this should be done once for all clients in the process, when a shared notification channel
  112. // will be shared by all clients...
  113. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value1, int value2)
  114. {
  115. int res = 0;
  116. // Done all time
  117. switch (notify) {
  118. case kAddClient:
  119. jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
  120. // the synchro must be usable in I/O mode when several clients live in the same process
  121. res = fSynchroTable[refnum]->Connect(name, fServerName) ? 0 : -1;
  122. break;
  123. case kRemoveClient:
  124. jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
  125. if (strcmp(GetClientControl()->fName, name) != 0)
  126. res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
  127. break;
  128. }
  129. return res;
  130. }
  131. JackGraphManager* JackLibClient::GetGraphManager() const
  132. {
  133. assert(JackLibGlobals::fGlobals->fGraphManager);
  134. return JackLibGlobals::fGlobals->fGraphManager;
  135. }
  136. JackEngineControl* JackLibClient::GetEngineControl() const
  137. {
  138. assert(JackLibGlobals::fGlobals->fEngineControl);
  139. return JackLibGlobals::fGlobals->fEngineControl;
  140. }
  141. JackClientControl* JackLibClient::GetClientControl() const
  142. {
  143. return fClientControl;
  144. }
  145. } // end of namespace