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.

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