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.

175 lines
5.1KB

  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* server_name, const char* name, jack_options_t options, jack_status_t* status)
  52. {
  53. int shared_engine, shared_client, shared_graph, result;
  54. JackLog("JackLibClient::Open %s\n", name);
  55. snprintf(fServerName, sizeof(fServerName), server_name);
  56. // Open server/client channel
  57. char name_res[JACK_CLIENT_NAME_SIZE];
  58. if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
  59. jack_error("Cannot connect to the server");
  60. goto error;
  61. }
  62. // Start receiving notifications
  63. if (fChannel->Start() < 0) {
  64. jack_error("Cannot start channel");
  65. goto error;
  66. }
  67. // Require new client
  68. fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
  69. if (result < 0) {
  70. jack_error("Cannot open %s client", name_res);
  71. goto error;
  72. }
  73. try {
  74. // Map shared memory segments
  75. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  76. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  77. fClientControl.SetShmIndex(shared_client, fServerName);
  78. jack_verbose = GetEngineControl()->fVerbose;
  79. } catch (int n) {
  80. jack_error("Map shared memory segments exception %d", n);
  81. goto error;
  82. }
  83. SetupDriverSync(false);
  84. /* TODO : solve WIN32 thread Kill issue
  85. #ifndef WIN32
  86. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  87. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
  88. jack_error("Cannot ConnectSemaphore %s client", name);
  89. goto error;
  90. }
  91. #endif
  92. */
  93. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  94. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name_res, fServerName)) {
  95. jack_error("Cannot ConnectSemaphore %s client", name_res);
  96. goto error;
  97. }
  98. JackLog("JackLibClient::Open name = %s refnum = %ld\n", name_res, fClientControl->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 value)
  109. {
  110. int res = 0;
  111. // Done all time
  112. switch (notify) {
  113. case kAddClient:
  114. JackLog("JackClient::AddClient name = %s, ref = %ld \n", 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. JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", 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. } // end of namespace