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
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. return (JackLibGlobals::fGlobals) ? JackLibGlobals::fGlobals->fGraphManager : NULL;
  26. }
  27. JackEngineControl* GetEngineControl()
  28. {
  29. return (JackLibGlobals::fGlobals) ? JackLibGlobals::fGlobals->fEngineControl : NULL;
  30. }
  31. JackSynchro** GetSynchroTable()
  32. {
  33. return (JackLibGlobals::fGlobals) ? JackLibGlobals::fGlobals->fSynchroTable : NULL;
  34. }
  35. //-------------------
  36. // Client management
  37. //-------------------
  38. JackLibClient::JackLibClient(JackSynchro** table): JackClient(table)
  39. {
  40. jack_log("JackLibClient::JackLibClient table = %x", table);
  41. fChannel = JackGlobals::MakeClientChannel();
  42. }
  43. JackLibClient::~JackLibClient()
  44. {
  45. jack_log("JackLibClient::~JackLibClient");
  46. delete fChannel;
  47. }
  48. int JackLibClient::Open(const char* server_name, const char* name, jack_options_t options, jack_status_t* status)
  49. {
  50. int shared_engine, shared_client, shared_graph, result;
  51. jack_log("JackLibClient::Open %s", name);
  52. snprintf(fServerName, sizeof(fServerName), server_name);
  53. // Open server/client channel
  54. char name_res[JACK_CLIENT_NAME_SIZE];
  55. if (fChannel->Open(server_name, name, name_res, this, options, status) < 0) {
  56. jack_error("Cannot connect to the server");
  57. goto error;
  58. }
  59. // Start receiving notifications
  60. if (fChannel->Start() < 0) {
  61. jack_error("Cannot start channel");
  62. goto error;
  63. }
  64. // Require new client
  65. fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
  66. if (result < 0) {
  67. jack_error("Cannot open %s client", name_res);
  68. goto error;
  69. }
  70. try {
  71. // Map shared memory segments
  72. JackLibGlobals::fGlobals->fEngineControl.SetShmIndex(shared_engine, fServerName);
  73. JackLibGlobals::fGlobals->fGraphManager.SetShmIndex(shared_graph, fServerName);
  74. fClientControl.SetShmIndex(shared_client, fServerName);
  75. jack_verbose = GetEngineControl()->fVerbose;
  76. } catch (int n) {
  77. jack_error("Map shared memory segments exception %d", n);
  78. goto error;
  79. }
  80. SetupDriverSync(false);
  81. /* TODO : solve WIN32 thread Kill issue
  82. #ifndef WIN32
  83. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  84. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
  85. jack_error("Cannot ConnectSemaphore %s client", name);
  86. goto error;
  87. }
  88. #endif
  89. */
  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