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.

825 lines
26KB

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