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.

173 lines
4.9KB

  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, 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. // Open server/client channel
  56. char name_res[JACK_CLIENT_NAME_SIZE];
  57. if (fChannel->Open(name, name_res, this, options, status) < 0) {
  58. jack_error("Cannot connect to the server");
  59. goto error;
  60. }
  61. // Start receiving notifications
  62. if (fChannel->Start() < 0) {
  63. jack_error("Cannot start channel");
  64. goto error;
  65. }
  66. // Require new client
  67. fChannel->ClientOpen(name_res, &shared_engine, &shared_client, &shared_graph, &result);
  68. if (result < 0) {
  69. jack_error("Cannot open %s client", name_res);
  70. goto error;
  71. }
  72. try {
  73. // Map shared memory segments
  74. JackLibGlobals::fGlobals->fEngineControl = shared_engine;
  75. JackLibGlobals::fGlobals->fGraphManager = shared_graph;
  76. fClientControl = shared_client;
  77. jack_verbose = GetEngineControl()->fVerbose;
  78. } catch (int n) {
  79. jack_error("Map shared memory segments exception %d", n);
  80. goto error;
  81. }
  82. SetupDriverSync(false);
  83. /* TODO : solve WIN32 thread Kill issue
  84. #ifndef WIN32
  85. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  86. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name)) {
  87. jack_error("Cannot ConnectSemaphore %s client", name);
  88. goto error;
  89. }
  90. #endif
  91. */
  92. // Connect shared synchro : the synchro must be usable in I/O mode when several clients live in the same process
  93. if (!fSynchroTable[fClientControl->fRefNum]->Connect(name_res)) {
  94. jack_error("Cannot ConnectSemaphore %s client", name_res);
  95. goto error;
  96. }
  97. JackLog("JackLibClient::Open name = %s refnum = %ld\n", name_res, fClientControl->fRefNum);
  98. return 0;
  99. error:
  100. fChannel->Stop();
  101. fChannel->Close();
  102. return -1;
  103. }
  104. // Notifications received from the server
  105. // TODO this should be done once for all clients in the process, when a shared notification channel
  106. // will be shared by all clients...
  107. int JackLibClient::ClientNotifyImp(int refnum, const char* name, int notify, int sync, int value)
  108. {
  109. int res = 0;
  110. // Done all time
  111. switch (notify) {
  112. case kAddClient:
  113. JackLog("JackClient::AddClient name = %s, ref = %ld \n", name, refnum);
  114. // the synchro must be usable in I/O mode when several clients live in the same process
  115. res = fSynchroTable[refnum]->Connect(name) ? 0 : -1;
  116. break;
  117. case kRemoveClient:
  118. JackLog("JackClient::RemoveClient name = %s, ref = %ld \n", name, refnum);
  119. if (strcmp(GetClientControl()->fName, name) != 0)
  120. res = fSynchroTable[refnum]->Disconnect() ? 0 : -1;
  121. break;
  122. }
  123. return res;
  124. }
  125. JackGraphManager* JackLibClient::GetGraphManager() const
  126. {
  127. assert(JackLibGlobals::fGlobals->fGraphManager);
  128. return JackLibGlobals::fGlobals->fGraphManager;
  129. }
  130. JackEngineControl* JackLibClient::GetEngineControl() const
  131. {
  132. assert(JackLibGlobals::fGlobals->fEngineControl);
  133. return JackLibGlobals::fGlobals->fEngineControl;
  134. }
  135. JackClientControl* JackLibClient::GetClientControl() const
  136. {
  137. return fClientControl;
  138. }
  139. } // end of namespace