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.

816 lines
25KB

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