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.

163 lines
4.5KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. assert(JackLibGlobals::fGlobals->fGraphManager);
  26. return JackLibGlobals::fGlobals->fGraphManager;
  27. }
  28. JackEngineControl* GetEngineControl()
  29. {
  30. assert(JackLibGlobals::fGlobals->fEngineControl);
  31. return JackLibGlobals::fGlobals->fEngineControl;
  32. }
  33. JackSynchro** GetSynchroTable()
  34. {
  35. assert(JackLibGlobals::fGlobals);
  36. return JackLibGlobals::fGlobals->fSynchroTable;
  37. }
  38. //-------------------
  39. // Client management
  40. //-------------------
  41. JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
  42. {
  43. JackLog("JackLibClient::JackLibClient table = %x\n", table);
  44. fChannel = JackGlobals::MakeClientChannel();
  45. }
  46. JackLibClient::~JackLibClient()
  47. {
  48. JackLog("JackLibClient::~JackLibClient\n");
  49. delete fChannel;
  50. }
  51. int JackLibClient::Open(const char* name)
  52. {
  53. int shared_engine, shared_client, shared_ports, result;
  54. JackLog("JackLibClient::Open %s\n", name);
  55. // Open server/client channel
  56. if (fChannel->Open(name, this) < 0) {
  57. jack_error("Cannot connect to the server");
  58. goto error;
  59. }
  60. // Start receiving notifications
  61. if (fChannel->Start() < 0) {
  62. jack_error("Cannot start channel");
  63. goto error;
  64. }
  65. // Require new client
  66. fChannel->ClientNew(name, &shared_engine, &shared_client, &shared_ports, &result);
  67. if (result < 0) {
  68. jack_error("Cannot open %s client", name);
  69. goto error;
  70. }
  71. try {
  72. // Map shared memory segments
  73. JackLibGlobals::fGlobals->fEngineControl = shared_engine;
  74. JackLibGlobals::fGlobals->fGraphManager = shared_ports;
  75. fClientControl = shared_client;
  76. verbose = GetEngineControl()->fVerbose;
  77. } catch (int n) {
  78. jack_error("Map shared memory segments exception %d", n);
  79. goto error;
  80. }
  81. SetupDriverSync(false);
  82. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  83. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
  84. jack_error("Cannot ConnectSemaphore %s client", name);
  85. goto error;
  86. }
  87. JackLog("JackLibClient::Open: name, refnum %s %ld\n", name, fClientControl->fRefNum);
  88. return 0;
  89. error:
  90. fChannel->Stop();
  91. fChannel->Close();
  92. return -1;
  93. }
  94. // Notifications received from the server
  95. // TODO this should be done once for all clients in the process, when a shared notification channel
  96. // will be shared by all clients...
  97. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
  98. {
  99. int res = 0;
  100. // Done all time
  101. switch (notify) {
  102. case JackNotifyChannelInterface::kAddClient:
  103. JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
  104. // the synchro must be usable in I/O mode when several clients live in the same process
  105. res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
  106. break;
  107. case JackNotifyChannelInterface::kRemoveClient:
  108. JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
  109. if (strcmp(GetClientControl()->fName, name) != 0)
  110. res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
  111. break;
  112. }
  113. return res;
  114. }
  115. JackGraphManager* JackLibClient::GetGraphManager() const
  116. {
  117. assert(JackLibGlobals::fGlobals->fGraphManager);
  118. return JackLibGlobals::fGlobals->fGraphManager;
  119. }
  120. JackEngineControl* JackLibClient::GetEngineControl() const
  121. {
  122. assert(JackLibGlobals::fGlobals->fEngineControl);
  123. return JackLibGlobals::fGlobals->fEngineControl;
  124. }
  125. JackClientControl* JackLibClient::GetClientControl() const
  126. {
  127. return fClientControl;
  128. }
  129. } // end of namespace