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.

802 lines
24KB

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