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.

863 lines
27KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2008 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "JackGraphManager.h"
  17. #include "JackConstants.h"
  18. #include "JackError.h"
  19. #include <assert.h>
  20. #include <stdlib.h>
  21. #include <algorithm>
  22. #include <regex.h>
  23. namespace Jack
  24. {
  25. static void AssertBufferSize(jack_nframes_t buffer_size)
  26. {
  27. if (buffer_size > BUFFER_SIZE_MAX) {
  28. jack_log("JackGraphManager::AssertBufferSize frames = %ld", buffer_size);
  29. assert(buffer_size <= BUFFER_SIZE_MAX);
  30. }
  31. }
  32. void JackGraphManager::AssertPort(jack_port_id_t port_index)
  33. {
  34. if (port_index >= fPortMax) {
  35. jack_log("JackGraphManager::AssertPort port_index = %ld", port_index);
  36. assert(port_index < fPortMax);
  37. }
  38. }
  39. JackGraphManager* JackGraphManager::Allocate(int port_max)
  40. {
  41. // Using "Placement" new
  42. void* shared_ptr = JackShmMem::operator new(sizeof(JackGraphManager) + port_max * sizeof(JackPort));
  43. return new(shared_ptr) JackGraphManager(port_max);
  44. }
  45. void JackGraphManager::Destroy(JackGraphManager* manager)
  46. {
  47. // "Placement" new was used
  48. manager->~JackGraphManager();
  49. JackShmMem::operator delete(manager);
  50. }
  51. JackGraphManager::JackGraphManager(int port_max)
  52. {
  53. assert(port_max <= PORT_NUM_MAX);
  54. for (int i = 0; i < port_max; i++) {
  55. fPortArray[i].Release();
  56. }
  57. fPortMax = port_max;
  58. }
  59. JackPort* JackGraphManager::GetPort(jack_port_id_t port_index)
  60. {
  61. AssertPort(port_index);
  62. return &fPortArray[port_index];
  63. }
  64. float* JackGraphManager::GetBuffer(jack_port_id_t port_index)
  65. {
  66. return fPortArray[port_index].GetBuffer();
  67. }
  68. // Server
  69. void JackGraphManager::InitRefNum(int refnum)
  70. {
  71. JackConnectionManager* manager = WriteNextStateStart();
  72. manager->InitRefNum(refnum);
  73. WriteNextStateStop();
  74. }
  75. // RT
  76. void JackGraphManager::RunCurrentGraph()
  77. {
  78. JackConnectionManager* manager = ReadCurrentState();
  79. manager->ResetGraph(fClientTiming);
  80. }
  81. // RT
  82. bool JackGraphManager::RunNextGraph()
  83. {
  84. bool res;
  85. JackConnectionManager* manager = TrySwitchState(&res);
  86. manager->ResetGraph(fClientTiming);
  87. return res;
  88. }
  89. // RT
  90. bool JackGraphManager::IsFinishedGraph()
  91. {
  92. JackConnectionManager* manager = ReadCurrentState();
  93. return (manager->GetActivation(FREEWHEEL_DRIVER_REFNUM) == 0);
  94. }
  95. // RT
  96. int JackGraphManager::ResumeRefNum(JackClientControl* control, JackSynchro* table)
  97. {
  98. JackConnectionManager* manager = ReadCurrentState();
  99. return manager->ResumeRefNum(control, table, fClientTiming);
  100. }
  101. // RT
  102. int JackGraphManager::SuspendRefNum(JackClientControl* control, JackSynchro* table, long usec)
  103. {
  104. JackConnectionManager* manager = ReadCurrentState();
  105. return manager->SuspendRefNum(control, table, fClientTiming, usec);
  106. }
  107. // Server
  108. void JackGraphManager::DirectConnect(int ref1, int ref2)
  109. {
  110. JackConnectionManager* manager = WriteNextStateStart();
  111. manager->DirectConnect(ref1, ref2);
  112. jack_log("JackGraphManager::ConnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  113. WriteNextStateStop();
  114. }
  115. // Server
  116. void JackGraphManager::DirectDisconnect(int ref1, int ref2)
  117. {
  118. JackConnectionManager* manager = WriteNextStateStart();
  119. manager->DirectDisconnect(ref1, ref2);
  120. jack_log("JackGraphManager::DisconnectRefNum cur_index = %ld ref1 = %ld ref2 = %ld", CurIndex(fCounter), ref1, ref2);
  121. WriteNextStateStop();
  122. }
  123. // Server
  124. bool JackGraphManager::IsDirectConnection(int ref1, int ref2)
  125. {
  126. JackConnectionManager* manager = ReadCurrentState();
  127. return manager->IsDirectConnection(ref1, ref2);
  128. }
  129. // RT
  130. void* JackGraphManager::GetBuffer(jack_port_id_t port_index, jack_nframes_t buffer_size)
  131. {
  132. AssertPort(port_index);
  133. AssertBufferSize(buffer_size);
  134. JackConnectionManager* manager = ReadCurrentState();
  135. JackPort* port = GetPort(port_index);
  136. // This happens when a port has just been unregistered and is still used by the RT code
  137. if (!port->IsUsed()) {
  138. jack_log("JackGraphManager::GetBuffer : port = %ld is released state", port_index);
  139. return GetBuffer(0); // port_index 0 is not used
  140. }
  141. // Output port
  142. if (port->fFlags & JackPortIsOutput) {
  143. return (port->fTied != NO_PORT) ? GetBuffer(port->fTied, buffer_size) : GetBuffer(port_index);
  144. }
  145. // Input port
  146. jack_int_t len = manager->Connections(port_index);
  147. // No connections : return a zero-filled buffer
  148. if (len == 0) {
  149. port->ClearBuffer(buffer_size);
  150. return port->GetBuffer();
  151. // One connection
  152. } else if (len == 1) {
  153. jack_port_id_t src_index = manager->GetPort(port_index, 0);
  154. // Ports in same client : copy the buffer
  155. if (GetPort(src_index)->GetRefNum() == port->GetRefNum()) {
  156. void* buffers[1];
  157. buffers[0] = GetBuffer(src_index, buffer_size);
  158. port->MixBuffers(buffers, 1, buffer_size);
  159. return port->GetBuffer();
  160. // Otherwise, use zero-copy mode, just pass the buffer of the connected (output) port.
  161. } else {
  162. return GetBuffer(src_index, buffer_size);
  163. }
  164. // Multiple connections : mix all buffers
  165. } else {
  166. const jack_int_t* connections = manager->GetConnections(port_index);
  167. void* buffers[CONNECTION_NUM_FOR_PORT];
  168. jack_port_id_t src_index;
  169. int i;
  170. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  171. AssertPort(src_index);
  172. buffers[i] = GetBuffer(src_index, buffer_size);
  173. }
  174. port->MixBuffers(buffers, i, buffer_size);
  175. return port->GetBuffer();
  176. }
  177. }
  178. // Server
  179. int JackGraphManager::RequestMonitor(jack_port_id_t port_index, bool onoff) // Client
  180. {
  181. AssertPort(port_index);
  182. JackPort* port = GetPort(port_index);
  183. /**
  184. jackd.h
  185. * If @ref JackPortCanMonitor is set for this @a port, turn input
  186. * monitoring on or off. Otherwise, do nothing.
  187. if (!(fFlags & JackPortCanMonitor))
  188. return -1;
  189. */
  190. port->RequestMonitor(onoff);
  191. const jack_int_t* connections = ReadCurrentState()->GetConnections(port_index);
  192. if ((port->fFlags & JackPortIsOutput) == 0) { // ?? Taken from jack, why not (port->fFlags & JackPortIsInput) ?
  193. jack_port_id_t src_index;
  194. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((src_index = connections[i]) != EMPTY); i++) {
  195. // XXX much worse things will happen if there is a feedback loop !!!
  196. RequestMonitor(src_index, onoff);
  197. }
  198. }
  199. return 0;
  200. }
  201. // Client
  202. jack_nframes_t JackGraphManager::ComputeTotalLatencyAux(jack_port_id_t port_index, jack_port_id_t src_port_index, JackConnectionManager* manager, int hop_count)
  203. {
  204. const jack_int_t* connections = manager->GetConnections(port_index);
  205. jack_nframes_t max_latency = 0;
  206. jack_port_id_t dst_index;
  207. if (hop_count > 8)
  208. return GetPort(port_index)->GetLatency();
  209. for (int i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((dst_index = connections[i]) != EMPTY); i++) {
  210. if (src_port_index != dst_index) {
  211. AssertPort(dst_index);
  212. JackPort* dst_port = GetPort(dst_index);
  213. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  214. ? dst_port->GetLatency()
  215. : ComputeTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  216. max_latency = ((max_latency > this_latency) ? max_latency : this_latency);
  217. }
  218. }
  219. return max_latency + GetPort(port_index)->GetLatency();
  220. }
  221. // Client
  222. int JackGraphManager::ComputeTotalLatency(jack_port_id_t port_index)
  223. {
  224. UInt16 cur_index;
  225. UInt16 next_index;
  226. JackPort* port = GetPort(port_index);
  227. AssertPort(port_index);
  228. do {
  229. cur_index = GetCurrentIndex();
  230. port->fTotalLatency = ComputeTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  231. next_index = GetCurrentIndex();
  232. } while (cur_index != next_index); // Until a coherent state has been read
  233. jack_log("JackGraphManager::GetTotalLatency port_index = %ld total latency = %ld", port_index, port->fTotalLatency);
  234. return 0;
  235. }
  236. // Client
  237. int JackGraphManager::ComputeTotalLatencies()
  238. {
  239. jack_port_id_t port_index;
  240. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  241. JackPort* port = GetPort(port_index);
  242. if (port->IsUsed())
  243. ComputeTotalLatency(port_index);
  244. }
  245. return 0;
  246. }
  247. // Server
  248. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  249. {
  250. jack_log("JackGraphManager::SetBufferSize size = %ld", buffer_size);
  251. jack_port_id_t port_index;
  252. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  253. JackPort* port = GetPort(port_index);
  254. if (port->IsUsed())
  255. port->ClearBuffer(buffer_size);
  256. }
  257. }
  258. // Server
  259. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  260. {
  261. jack_port_id_t port_index;
  262. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  263. for (port_index = FIRST_AVAILABLE_PORT; port_index < fPortMax; port_index++) {
  264. JackPort* port = GetPort(port_index);
  265. if (!port->IsUsed()) {
  266. jack_log("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s", port_index, port_name, port_type);
  267. if (!port->Allocate(refnum, port_name, port_type, flags))
  268. return NO_PORT;
  269. break;
  270. }
  271. }
  272. return (port_index < fPortMax) ? port_index : NO_PORT;
  273. }
  274. // Server
  275. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags, jack_nframes_t buffer_size)
  276. {
  277. JackConnectionManager* manager = WriteNextStateStart();
  278. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  279. if (port_index != NO_PORT) {
  280. JackPort* port = GetPort(port_index);
  281. assert(port);
  282. port->ClearBuffer(buffer_size);
  283. int res;
  284. if (flags & JackPortIsOutput) {
  285. res = manager->AddOutputPort(refnum, port_index);
  286. } else {
  287. res = manager->AddInputPort(refnum, port_index);
  288. }
  289. // Insertion failure
  290. if (res < 0) {
  291. port->Release();
  292. port_index = NO_PORT;
  293. }
  294. }
  295. WriteNextStateStop();
  296. return port_index;
  297. }
  298. // Server
  299. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  300. {
  301. JackConnectionManager* manager = WriteNextStateStart();
  302. JackPort* port = GetPort(port_index);
  303. int res;
  304. if (port->fFlags & JackPortIsOutput) {
  305. DisconnectAllOutput(port_index);
  306. res = manager->RemoveOutputPort(refnum, port_index);
  307. } else {
  308. DisconnectAllInput(port_index);
  309. res = manager->RemoveInputPort(refnum, port_index);
  310. }
  311. port->Release();
  312. WriteNextStateStop();
  313. return res;
  314. }
  315. void JackGraphManager::ActivatePort(jack_port_id_t port_index)
  316. {
  317. JackPort* port = GetPort(port_index);
  318. port->fFlags = (JackPortFlags)(port->fFlags | JackPortIsActive);
  319. }
  320. void JackGraphManager::DeactivatePort(jack_port_id_t port_index)
  321. {
  322. JackPort* port = GetPort(port_index);
  323. port->fFlags = (JackPortFlags)(port->fFlags | ~JackPortIsActive);
  324. }
  325. void JackGraphManager::GetInputPorts(int refnum, jack_int_t* res)
  326. {
  327. JackConnectionManager* manager = WriteNextStateStart();
  328. const jack_int_t* input = manager->GetInputPorts(refnum);
  329. memcpy(res, input, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  330. WriteNextStateStop();
  331. }
  332. void JackGraphManager::GetOutputPorts(int refnum, jack_int_t* res)
  333. {
  334. JackConnectionManager* manager = WriteNextStateStart();
  335. const jack_int_t* output = manager->GetOutputPorts(refnum);
  336. memcpy(res, output, sizeof(jack_int_t) * PORT_NUM_FOR_CLIENT);
  337. WriteNextStateStop();
  338. }
  339. // Server
  340. void JackGraphManager::RemoveAllPorts(int refnum)
  341. {
  342. jack_log("JackGraphManager::RemoveAllPorts ref = %ld", refnum);
  343. JackConnectionManager* manager = WriteNextStateStart();
  344. jack_port_id_t port_index;
  345. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "input" table is empty
  346. const jack_int_t* input = manager->GetInputPorts(refnum);
  347. while ((port_index = input[0]) != EMPTY) {
  348. int res = ReleasePort(refnum, port_index);
  349. if (res < 0) {
  350. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  351. assert(true);
  352. break;
  353. }
  354. }
  355. // Warning : ReleasePort shift port to left, thus we always remove the first port until the "output" table is empty
  356. const jack_int_t* output = manager->GetOutputPorts(refnum);
  357. while ((port_index = output[0]) != EMPTY) {
  358. int res = ReleasePort(refnum, port_index);
  359. if (res < 0) {
  360. jack_error("JackGraphManager::RemoveAllPorts failure ref = %ld port_index = %ld", refnum, port_index);
  361. assert(true);
  362. break;
  363. }
  364. }
  365. WriteNextStateStop();
  366. }
  367. // Server
  368. void JackGraphManager::DisconnectAllPorts(int refnum)
  369. {
  370. int i;
  371. jack_log("JackGraphManager::DisconnectAllPorts ref = %ld", refnum);
  372. JackConnectionManager* manager = WriteNextStateStart();
  373. const jack_int_t* input = manager->GetInputPorts(refnum);
  374. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  375. DisconnectAllInput(input[i]);
  376. }
  377. const jack_int_t* output = manager->GetOutputPorts(refnum);
  378. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  379. DisconnectAllOutput(output[i]);
  380. }
  381. WriteNextStateStop();
  382. }
  383. // Server
  384. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  385. {
  386. jack_log("JackGraphManager::DisconnectAllInput port_index = %ld", port_index);
  387. JackConnectionManager* manager = WriteNextStateStart();
  388. for (unsigned int i = 0; i < fPortMax; i++) {
  389. if (manager->IsConnected(i, port_index)) {
  390. jack_log("JackGraphManager::Disconnect i = %ld port_index = %ld", i, port_index);
  391. Disconnect(i, port_index);
  392. }
  393. }
  394. WriteNextStateStop();
  395. }
  396. // Server
  397. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  398. {
  399. jack_log("JackGraphManager::DisconnectAllOutput port_index = %ld ", port_index);
  400. JackConnectionManager* manager = WriteNextStateStart();
  401. const jack_int_t* connections = manager->GetConnections(port_index);
  402. while (connections[0] != EMPTY) {
  403. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  404. }
  405. WriteNextStateStop();
  406. }
  407. // Server
  408. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  409. {
  410. AssertPort(port_index);
  411. JackPort* port = GetPort(port_index);
  412. if (port->fFlags & JackPortIsOutput) {
  413. DisconnectAllOutput(port_index);
  414. } else {
  415. DisconnectAllInput(port_index);
  416. }
  417. return 0;
  418. }
  419. // Server
  420. void JackGraphManager::GetConnections(jack_port_id_t port_index, jack_int_t* res)
  421. {
  422. JackConnectionManager* manager = WriteNextStateStart();
  423. const jack_int_t* connections = manager->GetConnections(port_index);
  424. memcpy(res, connections, sizeof(jack_int_t) * CONNECTION_NUM_FOR_PORT);
  425. WriteNextStateStop();
  426. }
  427. // Server
  428. void JackGraphManager::Activate(int refnum)
  429. {
  430. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  431. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  432. }
  433. /*
  434. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  435. (thus unactivated) state may happen where the client is still checked for its end.
  436. */
  437. // Server
  438. void JackGraphManager::Deactivate(int refnum)
  439. {
  440. // Disconnect only when needed
  441. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  442. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  443. } else {
  444. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  445. }
  446. // Disconnect only when needed
  447. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  448. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  449. } else {
  450. jack_log("JackServer::Deactivate client = %ld was not activated", refnum);
  451. }
  452. }
  453. // Server
  454. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  455. {
  456. AssertPort(port_index);
  457. JackConnectionManager* manager = WriteNextStateStart();
  458. int res = manager->GetInputRefNum(port_index);
  459. WriteNextStateStop();
  460. return res;
  461. }
  462. // Server
  463. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  464. {
  465. AssertPort(port_index);
  466. JackConnectionManager* manager = WriteNextStateStart();
  467. int res = manager->GetOutputRefNum(port_index);
  468. WriteNextStateStop();
  469. return res;
  470. }
  471. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  472. {
  473. JackConnectionManager* manager = WriteNextStateStart();
  474. jack_log("JackGraphManager::Connect port_src = %ld port_dst = %ld", port_src, port_dst);
  475. JackPort* src = GetPort(port_src);
  476. JackPort* dst = GetPort(port_dst);
  477. int res = 0;
  478. if (!src->fInUse || !dst->fInUse) {
  479. if (!src->fInUse)
  480. jack_error("JackGraphManager::Connect port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  481. if (!dst->fInUse)
  482. jack_error("JackGraphManager::Connect port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  483. res = -1;
  484. goto end;
  485. }
  486. if (src->fTypeId != dst->fTypeId) {
  487. jack_error("JackGraphManager::Connect different port types port_src = %ld port_dst = %ld", port_src, port_dst);
  488. res = -1;
  489. goto end;
  490. }
  491. if (manager->IsConnected(port_src, port_dst)) {
  492. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  493. res = EEXIST;
  494. goto end;
  495. }
  496. res = manager->Connect(port_src, port_dst);
  497. if (res < 0) {
  498. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  499. goto end;
  500. }
  501. res = manager->Connect(port_dst, port_src);
  502. if (res < 0) {
  503. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  504. goto end;
  505. }
  506. if (manager->IsLoopPath(port_src, port_dst)) {
  507. jack_log("JackGraphManager::Connect: LOOP detected");
  508. manager->IncFeedbackConnection(port_src, port_dst);
  509. } else {
  510. manager->IncDirectConnection(port_src, port_dst);
  511. }
  512. end:
  513. WriteNextStateStop();
  514. return res;
  515. }
  516. // Server
  517. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  518. {
  519. JackConnectionManager* manager = WriteNextStateStart();
  520. jack_log("JackGraphManager::Disconnect port_src = %ld port_dst = %ld", port_src, port_dst);
  521. bool in_use_src = GetPort(port_src)->fInUse;
  522. bool in_use_dst = GetPort(port_dst)->fInUse;
  523. int res = 0;
  524. if (!in_use_src || !in_use_dst) {
  525. if (!in_use_src)
  526. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  527. if (!in_use_dst)
  528. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  529. res = -1;
  530. goto end;
  531. }
  532. if (!manager->IsConnected(port_src, port_dst)) {
  533. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  534. res = -1;
  535. goto end;
  536. }
  537. res = manager->Disconnect(port_src, port_dst);
  538. if (res < 0) {
  539. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  540. goto end;
  541. }
  542. res = manager->Disconnect(port_dst, port_src);
  543. if (res < 0) {
  544. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  545. goto end;
  546. }
  547. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  548. jack_log("JackGraphManager::Disconnect: FEEDBACK removed");
  549. manager->DecFeedbackConnection(port_src, port_dst);
  550. } else {
  551. manager->DecDirectConnection(port_src, port_dst);
  552. }
  553. end:
  554. WriteNextStateStop();
  555. return res;
  556. }
  557. // Client
  558. int JackGraphManager::IsConnected(jack_port_id_t port_src, jack_port_id_t port_dst)
  559. {
  560. JackConnectionManager* manager = ReadCurrentState();
  561. return manager->IsConnected(port_src, port_dst);
  562. }
  563. // Server
  564. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  565. {
  566. JackPort* src = GetPort(port_src);
  567. JackPort* dst = GetPort(port_dst);
  568. if ((dst->fFlags & JackPortIsInput) == 0) {
  569. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  570. return -1;
  571. }
  572. if ((src->fFlags & JackPortIsOutput) == 0) {
  573. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  574. return -1;
  575. }
  576. return 0;
  577. }
  578. int JackGraphManager::GetTwoPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  579. {
  580. jack_log("JackGraphManager::CheckConnect src_name = %s dst_name = %s", src_name, dst_name);
  581. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  582. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  583. return -1;
  584. }
  585. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  586. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  587. return -1;
  588. }
  589. return 0;
  590. }
  591. // Client : port array
  592. jack_port_id_t JackGraphManager::GetPort(const char* name)
  593. {
  594. for (unsigned int i = 0; i < fPortMax; i++) {
  595. JackPort* port = GetPort(i);
  596. if (port->IsUsed() && port->NameEquals(name))
  597. return i;
  598. }
  599. return NO_PORT;
  600. }
  601. /*!
  602. \brief Get the connection port name array.
  603. */
  604. // Client
  605. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  606. {
  607. const jack_int_t* connections = manager->GetConnections(port_index);
  608. jack_int_t index;
  609. int i;
  610. // Cleanup connection array
  611. memset(res, 0, sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  612. for (i = 0; (i < CONNECTION_NUM_FOR_PORT) && ((index = connections[i]) != EMPTY); i++) {
  613. JackPort* port = GetPort(index);
  614. res[i] = port->fName;
  615. }
  616. res[i] = NULL;
  617. }
  618. /*
  619. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  620. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  621. read to loop forever.
  622. */
  623. // Client
  624. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  625. {
  626. const char** res = (const char**)malloc(sizeof(char*) * CONNECTION_NUM_FOR_PORT);
  627. UInt16 cur_index, next_index;
  628. if (!res)
  629. return NULL;
  630. do {
  631. cur_index = GetCurrentIndex();
  632. GetConnectionsAux(ReadCurrentState(), res, port_index);
  633. next_index = GetCurrentIndex();
  634. } while (cur_index != next_index); // Until a coherent state has been read
  635. if (res[0]) { // at least one connection
  636. return res;
  637. } else { // empty array, should return NULL
  638. free(res);
  639. return NULL;
  640. }
  641. }
  642. // Client
  643. void JackGraphManager::GetPortsAux(const char** matching_ports, const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  644. {
  645. int match_cnt = 0;
  646. regex_t port_regex, type_regex;
  647. if (port_name_pattern && port_name_pattern[0]) {
  648. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  649. }
  650. if (type_name_pattern && type_name_pattern[0]) {
  651. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  652. }
  653. // Cleanup port array
  654. memset(matching_ports, 0, sizeof(char*) * fPortMax);
  655. for (unsigned int i = 0; i < fPortMax; i++) {
  656. bool matching = true;
  657. JackPort* port = GetPort(i);
  658. if (port->IsUsed()) {
  659. if (flags) {
  660. if ((port->fFlags & flags) != flags) {
  661. matching = false;
  662. }
  663. }
  664. if (matching && port_name_pattern && port_name_pattern[0]) {
  665. if (regexec(&port_regex, port->GetName(), 0, NULL, 0)) {
  666. matching = false;
  667. }
  668. }
  669. if (matching && type_name_pattern && type_name_pattern[0]) {
  670. if (regexec(&type_regex, port->GetType(), 0, NULL, 0)) {
  671. matching = false;
  672. }
  673. }
  674. if (matching) {
  675. matching_ports[match_cnt++] = port->fName;
  676. }
  677. }
  678. }
  679. matching_ports[match_cnt] = 0;
  680. if (port_name_pattern && port_name_pattern[0]) {
  681. regfree(&port_regex);
  682. }
  683. if (type_name_pattern && type_name_pattern[0]) {
  684. regfree(&type_regex);
  685. }
  686. }
  687. // Client
  688. /*
  689. Check that the state was not changed during the read operation.
  690. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  691. read to loop forever.
  692. */
  693. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  694. {
  695. const char** res = (const char**)malloc(sizeof(char*) * fPortMax);
  696. UInt16 cur_index, next_index;
  697. if (!res)
  698. return NULL;
  699. do {
  700. cur_index = GetCurrentIndex();
  701. GetPortsAux(res, port_name_pattern, type_name_pattern, flags);
  702. next_index = GetCurrentIndex();
  703. } while (cur_index != next_index); // Until a coherent state has been read
  704. if (res[0]) { // at least one port
  705. return res;
  706. } else {
  707. free(res); // empty array, should return NULL
  708. return NULL;
  709. }
  710. }
  711. // Server
  712. void JackGraphManager::Save(JackConnectionManager* dst)
  713. {
  714. JackConnectionManager* manager = WriteNextStateStart();
  715. memcpy(dst, manager, sizeof(JackConnectionManager));
  716. WriteNextStateStop();
  717. }
  718. // Server
  719. void JackGraphManager::Restore(JackConnectionManager* src)
  720. {
  721. JackConnectionManager* manager = WriteNextStateStart();
  722. memcpy(manager, src, sizeof(JackConnectionManager));
  723. WriteNextStateStop();
  724. }
  725. } // end of namespace