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.

820 lines
25KB

  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. // Server
  184. jack_nframes_t JackGraphManager::GetTotalLatencyAux(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 latency = GetPort(port_index)->GetLatency();
  188. jack_nframes_t max_latency = 0;
  189. jack_port_id_t dst_index;
  190. if (hop_count > 8)
  191. return latency;
  192. for (int i = 0; (i < CONNECTION_NUM) && ((dst_index = connections[i]) != EMPTY); i++) {
  193. if (src_port_index != dst_index) {
  194. AssertPort(dst_index);
  195. JackPort* dst_port = GetPort(dst_index);
  196. jack_nframes_t this_latency = (dst_port->fFlags & JackPortIsTerminal)
  197. ? dst_port->GetLatency()
  198. : GetTotalLatencyAux(dst_index, port_index, manager, hop_count + 1);
  199. max_latency = MAX(max_latency, this_latency);
  200. }
  201. }
  202. return max_latency + latency;
  203. }
  204. // Server
  205. jack_nframes_t JackGraphManager::GetTotalLatency(jack_port_id_t port_index)
  206. {
  207. UInt16 cur_index;
  208. UInt16 next_index;
  209. jack_nframes_t total_latency;
  210. AssertPort(port_index);
  211. JackLog("JackGraphManager::GetTotalLatency port_index = %ld\n", port_index);
  212. do {
  213. cur_index = GetCurrentIndex();
  214. total_latency = GetTotalLatencyAux(port_index, port_index, ReadCurrentState(), 0);
  215. next_index = GetCurrentIndex();
  216. } while (cur_index != next_index); // Until a coherent state has been read
  217. return total_latency;
  218. }
  219. // Server
  220. jack_port_id_t JackGraphManager::AllocatePortAux(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  221. {
  222. jack_port_id_t port_index;
  223. // Available ports start at FIRST_AVAILABLE_PORT (= 1), otherwise a port_index of 0 is "seen" as a NULL port by the external API...
  224. for (port_index = FIRST_AVAILABLE_PORT; port_index < PORT_NUM; port_index++) {
  225. JackPort* port = GetPort(port_index);
  226. if (!port->IsUsed()) {
  227. JackLog("JackGraphManager::AllocatePortAux port_index = %ld name = %s type = %s\n", port_index, port_name, port_type);
  228. if (!port->Allocate(refnum, port_name, port_type, flags))
  229. return NO_PORT;
  230. break;
  231. }
  232. }
  233. return (port_index < PORT_NUM) ? port_index : NO_PORT;
  234. }
  235. // Server
  236. jack_port_id_t JackGraphManager::AllocatePort(int refnum, const char* port_name, const char* port_type, JackPortFlags flags)
  237. {
  238. JackLock lock(this);
  239. JackConnectionManager* manager = WriteNextStateStart();
  240. jack_port_id_t port_index = AllocatePortAux(refnum, port_name, port_type, flags);
  241. assert(fBufferSize != 0);
  242. if (port_index != NO_PORT) {
  243. JackPort* port = GetPort(port_index);
  244. assert(port);
  245. port->ClearBuffer(fBufferSize);
  246. int res;
  247. if (flags & JackPortIsOutput) {
  248. res = manager->AddOutputPort(refnum, port_index);
  249. } else {
  250. res = manager->AddInputPort(refnum, port_index);
  251. }
  252. // Insertion failure
  253. if (res < 0) {
  254. port->Release();
  255. port_index = NO_PORT;
  256. }
  257. }
  258. WriteNextStateStop();
  259. return port_index;
  260. }
  261. // Server
  262. void JackGraphManager::SetBufferSize(jack_nframes_t buffer_size)
  263. {
  264. JackLock lock(this);
  265. JackLog("JackGraphManager::SetBufferSize size = %ld\n", (long int)buffer_size);
  266. jack_port_id_t port_index;
  267. fBufferSize = buffer_size;
  268. for (port_index = FIRST_AVAILABLE_PORT; port_index < PORT_NUM; port_index++) {
  269. JackPort* port = GetPort(port_index);
  270. if (port->IsUsed())
  271. port->ClearBuffer(fBufferSize);
  272. }
  273. }
  274. // Server
  275. int JackGraphManager::ReleasePort(int refnum, jack_port_id_t port_index)
  276. {
  277. JackLock lock(this);
  278. JackConnectionManager* manager = WriteNextStateStart();
  279. JackPort* port = GetPort(port_index);
  280. int res;
  281. if (port->fFlags & JackPortIsOutput) {
  282. DisconnectAllOutput(port_index);
  283. res = manager->RemoveOutputPort(refnum, port_index);
  284. } else {
  285. DisconnectAllInput(port_index);
  286. res = manager->RemoveInputPort(refnum, port_index);
  287. }
  288. port->Release();
  289. WriteNextStateStop();
  290. return res;
  291. }
  292. // Server
  293. void JackGraphManager::RemoveAllPorts(int refnum)
  294. {
  295. JackLog("JackGraphManager::RemoveAllPorts ref = %ld\n", refnum);
  296. JackConnectionManager* manager = WriteNextStateStart();
  297. jack_port_id_t port_index;
  298. // Warning : RemovePort shift port to left, thus we always remove the first port until the "input" table is empty
  299. const jack_int_t* input = manager->GetInputPorts(refnum);
  300. while ((port_index = input[0]) != EMPTY) {
  301. ReleasePort(refnum, port_index);
  302. }
  303. // Warning : RemovePort shift port to left, thus we always remove the first port until the "output" table is empty
  304. const jack_int_t* output = manager->GetOutputPorts(refnum);
  305. while ((port_index = output[0]) != EMPTY) {
  306. ReleasePort(refnum, port_index);
  307. }
  308. WriteNextStateStop();
  309. }
  310. // Server
  311. void JackGraphManager::DisconnectAllPorts(int refnum)
  312. {
  313. int i;
  314. JackLog("JackGraphManager::DisconnectAllPorts ref = %ld\n", refnum);
  315. JackConnectionManager* manager = WriteNextStateStart();
  316. const jack_int_t* input = manager->GetInputPorts(refnum);
  317. for (i = 0; i < PORT_NUM_FOR_CLIENT && input[i] != EMPTY ; i++) {
  318. DisconnectAllInput(input[i]);
  319. }
  320. const jack_int_t* output = manager->GetOutputPorts(refnum);
  321. for (i = 0; i < PORT_NUM_FOR_CLIENT && output[i] != EMPTY; i++) {
  322. DisconnectAllOutput(output[i]);
  323. }
  324. WriteNextStateStop();
  325. }
  326. // Server
  327. void JackGraphManager::DisconnectAllInput(jack_port_id_t port_index)
  328. {
  329. JackLog("JackGraphManager::DisconnectAllInput port_index = %ld \n", port_index);
  330. JackConnectionManager* manager = WriteNextStateStart();
  331. for (int i = 0; i < PORT_NUM; i++) {
  332. if (manager->IsConnected(i, port_index)) {
  333. JackLog("JackGraphManager::Disconnect i = %ld port_index = %ld\n", i, port_index);
  334. Disconnect(i, port_index);
  335. }
  336. }
  337. WriteNextStateStop();
  338. }
  339. // Server
  340. void JackGraphManager::DisconnectAllOutput(jack_port_id_t port_index)
  341. {
  342. JackLog("JackGraphManager::DisconnectAllOutput port_index = %ld \n", port_index);
  343. JackConnectionManager* manager = WriteNextStateStart();
  344. const jack_int_t* connections = manager->GetConnections(port_index);
  345. while (connections[0] != EMPTY) {
  346. Disconnect(port_index, connections[0]); // Warning : Disconnect shift port to left
  347. }
  348. WriteNextStateStop();
  349. }
  350. // Server
  351. int JackGraphManager::DisconnectAll(jack_port_id_t port_index)
  352. {
  353. AssertPort(port_index);
  354. JackPort* port = GetPort(port_index);
  355. if (port->fFlags & JackPortIsOutput) {
  356. DisconnectAllOutput(port_index);
  357. } else {
  358. DisconnectAllInput(port_index);
  359. }
  360. return 0;
  361. }
  362. // Server
  363. void JackGraphManager::Activate(int refnum)
  364. {
  365. DirectConnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  366. DirectConnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  367. }
  368. /*
  369. Disconnection from the FW must be done in last otherwise an intermediate "unconnected"
  370. (thus unactivated) state may happen where the client is still checked for its end.
  371. */
  372. // Server
  373. void JackGraphManager::Deactivate(int refnum)
  374. {
  375. DisconnectAllPorts(refnum);
  376. // Disconnect only when needed
  377. if (IsDirectConnection(refnum, FREEWHEEL_DRIVER_REFNUM)) {
  378. DirectDisconnect(refnum, FREEWHEEL_DRIVER_REFNUM);
  379. } else {
  380. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  381. }
  382. // Disconnect only when needed
  383. if (IsDirectConnection(FREEWHEEL_DRIVER_REFNUM, refnum)) {
  384. DirectDisconnect(FREEWHEEL_DRIVER_REFNUM, refnum);
  385. } else {
  386. JackLog("JackServer::Deactivate: client = %ld was not activated \n", refnum);
  387. }
  388. }
  389. // Server
  390. int JackGraphManager::GetInputRefNum(jack_port_id_t port_index)
  391. {
  392. AssertPort(port_index);
  393. JackConnectionManager* manager = WriteNextStateStart();
  394. int res = manager->GetInputRefNum(port_index);
  395. WriteNextStateStop();
  396. return res;
  397. }
  398. // Server
  399. int JackGraphManager::GetOutputRefNum(jack_port_id_t port_index)
  400. {
  401. AssertPort(port_index);
  402. JackConnectionManager* manager = WriteNextStateStart();
  403. int res = manager->GetOutputRefNum(port_index);
  404. WriteNextStateStop();
  405. return res;
  406. }
  407. int JackGraphManager::Connect(jack_port_id_t port_src, jack_port_id_t port_dst)
  408. {
  409. JackConnectionManager* manager = WriteNextStateStart();
  410. JackLog("JackGraphManager::Connect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  411. JackPort* src = GetPort(port_src);
  412. JackPort* dst = GetPort(port_dst);
  413. int res = 0;
  414. if (!src->fInUse || !dst->fInUse) {
  415. if (!src->fInUse)
  416. jack_error("JackGraphManager::Connect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  417. if (!dst->fInUse)
  418. jack_error("JackGraphManager::Connect: port_dst = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  419. res = -1;
  420. goto end;
  421. }
  422. if (src->fTypeId != dst->fTypeId) {
  423. jack_error("JackGraphManager::Connect: different port types: port_src = %ld port_dst = %ld", port_src, port_dst);
  424. res = -1;
  425. goto end;
  426. }
  427. if (manager->IsConnected(port_src, port_dst)) {
  428. jack_error("JackGraphManager::Connect already connected port_src = %ld port_dst = %ld", port_src, port_dst);
  429. res = EEXIST;
  430. goto end;
  431. }
  432. res = manager->Connect(port_src, port_dst);
  433. if (res < 0) {
  434. jack_error("JackGraphManager::Connect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  435. goto end;
  436. }
  437. manager->Connect(port_dst, port_src);
  438. if (res < 0) {
  439. jack_error("JackGraphManager::Connect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  440. goto end;
  441. }
  442. if (manager->IsLoopPath(port_src, port_dst)) {
  443. JackLog("JackGraphManager::Connect: LOOP detected\n");
  444. manager->IncFeedbackConnection(port_src, port_dst);
  445. } else {
  446. manager->IncDirectConnection(port_src, port_dst);
  447. }
  448. end:
  449. WriteNextStateStop();
  450. return res;
  451. }
  452. // Server
  453. int JackGraphManager::Disconnect(jack_port_id_t port_src, jack_port_id_t port_dst)
  454. {
  455. JackConnectionManager* manager = WriteNextStateStart();
  456. JackLog("JackGraphManager::Disconnect port_src = %ld port_dst = %ld\n", port_src, port_dst);
  457. bool in_use_src = GetPort(port_src)->fInUse;
  458. bool in_use_dst = GetPort(port_dst)->fInUse;
  459. int res = 0;
  460. if (!in_use_src || !in_use_dst) {
  461. if (!in_use_src)
  462. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_src, GetPort(port_src)->fName);
  463. if (!in_use_dst)
  464. jack_error("JackGraphManager::Disconnect: port_src = %ld not used name = %s", port_dst, GetPort(port_dst)->fName);
  465. res = -1;
  466. goto end;
  467. }
  468. if (!manager->IsConnected(port_src, port_dst)) {
  469. jack_error("JackGraphManager::Disconnect not connected port_src = %ld port_dst = %ld", port_src, port_dst);
  470. res = -1;
  471. goto end;
  472. }
  473. manager->Disconnect(port_src, port_dst);
  474. if (res < 0) {
  475. jack_error("JackGraphManager::Disconnect failed port_src = %ld port_dst = %ld", port_src, port_dst);
  476. goto end;
  477. }
  478. manager->Disconnect(port_dst, port_src);
  479. if (res < 0) {
  480. jack_error("JackGraphManager::Disconnect failed port_dst = %ld port_src = %ld", port_dst, port_src);
  481. goto end;
  482. }
  483. if (manager->IsFeedbackConnection(port_src, port_dst)) {
  484. JackLog("JackGraphManager::Disconnect: FEEDBACK removed\n");
  485. manager->DecFeedbackConnection(port_src, port_dst);
  486. } else {
  487. manager->DecDirectConnection(port_src, port_dst);
  488. }
  489. end:
  490. WriteNextStateStop();
  491. return res;
  492. }
  493. // Client
  494. int JackGraphManager::ConnectedTo(jack_port_id_t port_src, const char* port_name)
  495. {
  496. JackLog("JackGraphManager::ConnectedTo port_src = %ld port_name = %s\n", port_src, port_name);
  497. JackConnectionManager* manager = ReadCurrentState();
  498. jack_port_id_t port_dst;
  499. if ((port_dst = GetPort(port_name)) == NO_PORT) {
  500. jack_error("Unknown destination port port_name = %s", port_name);
  501. return 0;
  502. } else {
  503. return manager->IsConnected(port_src, port_dst);
  504. }
  505. }
  506. // Server
  507. int JackGraphManager::CheckPort(jack_port_id_t port_index)
  508. {
  509. JackPort* port = GetPort(port_index);
  510. if (port->fLocked) {
  511. jack_error("Port %s is locked against connection changes", port->fName);
  512. return -1;
  513. } else {
  514. return 0;
  515. }
  516. }
  517. int JackGraphManager::CheckPorts(jack_port_id_t port_src, jack_port_id_t port_dst)
  518. {
  519. JackPort* src = GetPort(port_src);
  520. JackPort* dst = GetPort(port_dst);
  521. if ((dst->Flags() & JackPortIsInput) == 0) {
  522. jack_error("Destination port in attempted (dis)connection of %s and %s is not an input port", src->fName, dst->fName);
  523. return -1;
  524. }
  525. if ((src->Flags() & JackPortIsOutput) == 0) {
  526. jack_error("Source port in attempted (dis)connection of %s and %s is not an output port", src->fName, dst->fName);
  527. return -1;
  528. }
  529. if (src->fLocked) {
  530. jack_error("Source port %s is locked against connection changes", src->fName);
  531. return -1;
  532. }
  533. if (dst->fLocked) {
  534. jack_error("Destination port %s is locked against connection changes", dst->fName);
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. int JackGraphManager::CheckPorts(const char* src_name, const char* dst_name, jack_port_id_t* port_src, jack_port_id_t* port_dst)
  540. {
  541. JackLog("JackGraphManager::CheckConnect src_name = %s dst_name = %s\n", src_name, dst_name);
  542. if ((*port_src = GetPort(src_name)) == NO_PORT) {
  543. jack_error("Unknown source port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  544. return -1;
  545. }
  546. if ((*port_dst = GetPort(dst_name)) == NO_PORT) {
  547. jack_error("Unknown destination port in attempted (dis)connection src_name [%s] dst_name [%s]", src_name, dst_name);
  548. return -1;
  549. }
  550. return CheckPorts(*port_src, *port_dst);
  551. }
  552. // Client : port array
  553. jack_port_id_t JackGraphManager::GetPort(const char* name)
  554. {
  555. for (int i = 0; i < PORT_NUM; i++) {
  556. JackPort* port = GetPort(i);
  557. if (port->IsUsed() && port->NameEquals(name))
  558. return i;
  559. }
  560. return NO_PORT;
  561. }
  562. /*!
  563. \brief Get the connection port name array.
  564. */
  565. // Client
  566. void JackGraphManager::GetConnectionsAux(JackConnectionManager* manager, const char** res, jack_port_id_t port_index)
  567. {
  568. const jack_int_t* connections = manager->GetConnections(port_index);
  569. jack_int_t index;
  570. int i;
  571. for (i = 0; (i < CONNECTION_NUM) && ((index = connections[i]) != EMPTY); i++) {
  572. JackPort* port = GetPort(index);
  573. res[i] = port->fName;
  574. }
  575. res[i] = NULL;
  576. }
  577. /*
  578. Use the state returned by ReadCurrentState and check that the state was not changed during the read operation.
  579. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  580. read to loop forever.
  581. */
  582. // Client
  583. const char** JackGraphManager::GetConnections(jack_port_id_t port_index)
  584. {
  585. const char** res = (const char**)malloc(sizeof(char*) * (CONNECTION_NUM + 1));
  586. UInt16 cur_index;
  587. UInt16 next_index;
  588. AssertPort(port_index);
  589. do {
  590. cur_index = GetCurrentIndex();
  591. GetConnectionsAux(ReadCurrentState(), res, port_index);
  592. next_index = GetCurrentIndex();
  593. } while (cur_index != next_index); // Until a coherent state has been read
  594. return res;
  595. }
  596. // Client
  597. const char** JackGraphManager::GetPortsAux(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  598. {
  599. const char** matching_ports;
  600. unsigned long match_cnt = 0;
  601. regex_t port_regex;
  602. regex_t type_regex;
  603. bool matching;
  604. if (port_name_pattern && port_name_pattern[0]) {
  605. regcomp(&port_regex, port_name_pattern, REG_EXTENDED | REG_NOSUB);
  606. }
  607. if (type_name_pattern && type_name_pattern[0]) {
  608. regcomp(&type_regex, type_name_pattern, REG_EXTENDED | REG_NOSUB);
  609. }
  610. matching_ports = (const char**)malloc(sizeof(char*) * PORT_NUM);
  611. for (int i = 0; i < PORT_NUM; i++) {
  612. matching = true;
  613. JackPort* port = GetPort(i);
  614. if (port->IsUsed()) {
  615. if (flags) {
  616. if ((port->fFlags & flags) != flags) {
  617. matching = false;
  618. }
  619. }
  620. if (matching && port_name_pattern && port_name_pattern[0]) {
  621. if (regexec(&port_regex, port->fName, 0, NULL, 0)) {
  622. matching = false;
  623. }
  624. }
  625. /*
  626. if (matching && type_name_pattern && type_name_pattern[0]) {
  627. jack_port_type_id_t ptid = psp[i].ptype_id;
  628. if (regexec (&type_regex,engine->port_types[ptid].type_name,0, NULL, 0)) {
  629. matching = false;
  630. }
  631. }
  632. */
  633. // TO BE IMPROVED
  634. if (matching && type_name_pattern && type_name_pattern[0]) {
  635. if (regexec(&type_regex, JACK_DEFAULT_AUDIO_TYPE, 0, NULL, 0)) {
  636. matching = false;
  637. }
  638. }
  639. if (matching) {
  640. matching_ports[match_cnt++] = port->fName;
  641. }
  642. }
  643. }
  644. matching_ports[match_cnt] = 0;
  645. if (match_cnt == 0) {
  646. free(matching_ports);
  647. matching_ports = NULL;
  648. }
  649. return matching_ports;
  650. }
  651. // Client
  652. /*
  653. Check that the state was not changed during the read operation.
  654. The operation is lock-free since there is no intermediate state in the write operation that could cause the
  655. read to loop forever.
  656. */
  657. const char** JackGraphManager::GetPorts(const char* port_name_pattern, const char* type_name_pattern, unsigned long flags)
  658. {
  659. const char** matching_ports = NULL;
  660. UInt16 cur_index;
  661. UInt16 next_index;
  662. do {
  663. cur_index = GetCurrentIndex();
  664. if (matching_ports) {
  665. free(matching_ports);
  666. JackLog("JackGraphManager::GetPorts retry... \n");
  667. }
  668. matching_ports = GetPortsAux(port_name_pattern, type_name_pattern, flags);
  669. next_index = GetCurrentIndex();
  670. } while (cur_index != next_index); // Until a coherent state has been read
  671. return matching_ports;
  672. }
  673. // Server
  674. void JackGraphManager::Save(JackConnectionManager* dst)
  675. {
  676. JackConnectionManager* manager = WriteNextStateStart();
  677. memcpy(dst, manager, sizeof(JackConnectionManager));
  678. WriteNextStateStop();
  679. }
  680. // Server
  681. void JackGraphManager::Restore(JackConnectionManager* src)
  682. {
  683. JackConnectionManager* manager = WriteNextStateStart();
  684. memcpy(manager, src, sizeof(JackConnectionManager));
  685. WriteNextStateStop();
  686. }
  687. } // end of namespace