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.

172 lines
4.9KB

  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. #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 = JackGlobals::MakeClientChannel();
  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 %s", name);
  61. snprintf(fServerName, sizeof(fServerName), server_name);
  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. jack_log("JackLibClient::Open name = %s refnum = %ld", name_res, GetClientControl()->fRefNum);
  96. return 0;
  97. error:
  98. fChannel->Stop();
  99. fChannel->Close();
  100. return -1;
  101. }
  102. // Notifications received from the server
  103. // TODO this should be done once for all clients in the process, when a shared notification channel
  104. // will be shared by all clients...
  105. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value1, int value2)
  106. {
  107. int res = 0;
  108. // Done all time
  109. switch (notify) {
  110. case kAddClient:
  111. jack_log("JackClient::AddClient name = %s, ref = %ld ", name, refnum);
  112. // the synchro must be usable in I/O mode when several clients live in the same process
  113. res = fSynchroTable[refnum]->Connect(name, fServerName) ? 0 : -1;
  114. break;
  115. case kRemoveClient:
  116. jack_log("JackClient::RemoveClient name = %s, ref = %ld ", name, refnum);
  117. if (strcmp(GetClientControl()->fName, name) != 0)
  118. res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
  119. break;
  120. }
  121. return res;
  122. }
  123. JackGraphManager* JackLibClient::GetGraphManager() const
  124. {
  125. assert(JackLibGlobals::fGlobals->fGraphManager);
  126. return JackLibGlobals::fGlobals->fGraphManager;
  127. }
  128. JackEngineControl* JackLibClient::GetEngineControl() const
  129. {
  130. assert(JackLibGlobals::fGlobals->fEngineControl);
  131. return JackLibGlobals::fGlobals->fEngineControl;
  132. }
  133. JackClientControl* JackLibClient::GetClientControl() const
  134. {
  135. return fClientControl;
  136. }
  137. } // end of namespace